misc.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * misc.c
  4. *
  5. * PURPOSE
  6. * Miscellaneous routines for the OSTA-UDF(tm) filesystem.
  7. *
  8. * COPYRIGHT
  9. * (C) 1998 Dave Boynton
  10. * (C) 1998-2004 Ben Fennema
  11. * (C) 1999-2000 Stelias Computing Inc
  12. *
  13. * HISTORY
  14. *
  15. * 04/19/99 blf partial support for reading/writing specific EA's
  16. */
  17. #include "udfdecl.h"
  18. #include <linux/fs.h>
  19. #include <linux/string.h>
  20. #include <linux/crc-itu-t.h>
  21. #include "udf_i.h"
  22. #include "udf_sb.h"
  23. struct genericFormat *udf_add_extendedattr(struct inode *inode, uint32_t size,
  24. uint32_t type, uint8_t loc)
  25. {
  26. uint8_t *ea = NULL, *ad = NULL;
  27. int offset;
  28. uint16_t crclen;
  29. struct udf_inode_info *iinfo = UDF_I(inode);
  30. ea = iinfo->i_data;
  31. if (iinfo->i_lenEAttr) {
  32. ad = iinfo->i_data + iinfo->i_lenEAttr;
  33. } else {
  34. ad = ea;
  35. size += sizeof(struct extendedAttrHeaderDesc);
  36. }
  37. offset = inode->i_sb->s_blocksize - udf_file_entry_alloc_offset(inode) -
  38. iinfo->i_lenAlloc;
  39. /* TODO - Check for FreeEASpace */
  40. if (loc & 0x01 && offset >= size) {
  41. struct extendedAttrHeaderDesc *eahd;
  42. eahd = (struct extendedAttrHeaderDesc *)ea;
  43. if (iinfo->i_lenAlloc)
  44. memmove(&ad[size], ad, iinfo->i_lenAlloc);
  45. if (iinfo->i_lenEAttr) {
  46. /* check checksum/crc */
  47. if (eahd->descTag.tagIdent !=
  48. cpu_to_le16(TAG_IDENT_EAHD) ||
  49. le32_to_cpu(eahd->descTag.tagLocation) !=
  50. iinfo->i_location.logicalBlockNum)
  51. return NULL;
  52. } else {
  53. struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
  54. size -= sizeof(struct extendedAttrHeaderDesc);
  55. iinfo->i_lenEAttr +=
  56. sizeof(struct extendedAttrHeaderDesc);
  57. eahd->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EAHD);
  58. if (sbi->s_udfrev >= 0x0200)
  59. eahd->descTag.descVersion = cpu_to_le16(3);
  60. else
  61. eahd->descTag.descVersion = cpu_to_le16(2);
  62. eahd->descTag.tagSerialNum =
  63. cpu_to_le16(sbi->s_serial_number);
  64. eahd->descTag.tagLocation = cpu_to_le32(
  65. iinfo->i_location.logicalBlockNum);
  66. eahd->impAttrLocation = cpu_to_le32(0xFFFFFFFF);
  67. eahd->appAttrLocation = cpu_to_le32(0xFFFFFFFF);
  68. }
  69. offset = iinfo->i_lenEAttr;
  70. if (type < 2048) {
  71. if (le32_to_cpu(eahd->appAttrLocation) <
  72. iinfo->i_lenEAttr) {
  73. uint32_t aal =
  74. le32_to_cpu(eahd->appAttrLocation);
  75. memmove(&ea[offset - aal + size],
  76. &ea[aal], offset - aal);
  77. offset -= aal;
  78. eahd->appAttrLocation =
  79. cpu_to_le32(aal + size);
  80. }
  81. if (le32_to_cpu(eahd->impAttrLocation) <
  82. iinfo->i_lenEAttr) {
  83. uint32_t ial =
  84. le32_to_cpu(eahd->impAttrLocation);
  85. memmove(&ea[offset - ial + size],
  86. &ea[ial], offset - ial);
  87. offset -= ial;
  88. eahd->impAttrLocation =
  89. cpu_to_le32(ial + size);
  90. }
  91. } else if (type < 65536) {
  92. if (le32_to_cpu(eahd->appAttrLocation) <
  93. iinfo->i_lenEAttr) {
  94. uint32_t aal =
  95. le32_to_cpu(eahd->appAttrLocation);
  96. memmove(&ea[offset - aal + size],
  97. &ea[aal], offset - aal);
  98. offset -= aal;
  99. eahd->appAttrLocation =
  100. cpu_to_le32(aal + size);
  101. }
  102. }
  103. /* rewrite CRC + checksum of eahd */
  104. crclen = sizeof(struct extendedAttrHeaderDesc) - sizeof(struct tag);
  105. eahd->descTag.descCRCLength = cpu_to_le16(crclen);
  106. eahd->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)eahd +
  107. sizeof(struct tag), crclen));
  108. eahd->descTag.tagChecksum = udf_tag_checksum(&eahd->descTag);
  109. iinfo->i_lenEAttr += size;
  110. return (struct genericFormat *)&ea[offset];
  111. }
  112. return NULL;
  113. }
  114. struct genericFormat *udf_get_extendedattr(struct inode *inode, uint32_t type,
  115. uint8_t subtype)
  116. {
  117. struct genericFormat *gaf;
  118. uint8_t *ea = NULL;
  119. uint32_t offset;
  120. struct udf_inode_info *iinfo = UDF_I(inode);
  121. ea = iinfo->i_data;
  122. if (iinfo->i_lenEAttr) {
  123. struct extendedAttrHeaderDesc *eahd;
  124. eahd = (struct extendedAttrHeaderDesc *)ea;
  125. /* check checksum/crc */
  126. if (eahd->descTag.tagIdent !=
  127. cpu_to_le16(TAG_IDENT_EAHD) ||
  128. le32_to_cpu(eahd->descTag.tagLocation) !=
  129. iinfo->i_location.logicalBlockNum)
  130. return NULL;
  131. if (type < 2048)
  132. offset = sizeof(struct extendedAttrHeaderDesc);
  133. else if (type < 65536)
  134. offset = le32_to_cpu(eahd->impAttrLocation);
  135. else
  136. offset = le32_to_cpu(eahd->appAttrLocation);
  137. while (offset + sizeof(*gaf) < iinfo->i_lenEAttr) {
  138. uint32_t attrLength;
  139. gaf = (struct genericFormat *)&ea[offset];
  140. attrLength = le32_to_cpu(gaf->attrLength);
  141. /* Detect undersized elements and buffer overflows */
  142. if ((attrLength < sizeof(*gaf)) ||
  143. (attrLength > (iinfo->i_lenEAttr - offset)))
  144. break;
  145. if (le32_to_cpu(gaf->attrType) == type &&
  146. gaf->attrSubtype == subtype)
  147. return gaf;
  148. else
  149. offset += attrLength;
  150. }
  151. }
  152. return NULL;
  153. }
  154. /*
  155. * udf_read_tagged
  156. *
  157. * PURPOSE
  158. * Read the first block of a tagged descriptor.
  159. *
  160. * HISTORY
  161. * July 1, 1997 - Andrew E. Mileski
  162. * Written, tested, and released.
  163. */
  164. struct buffer_head *udf_read_tagged(struct super_block *sb, uint32_t block,
  165. uint32_t location, uint16_t *ident)
  166. {
  167. struct tag *tag_p;
  168. struct buffer_head *bh = NULL;
  169. u8 checksum;
  170. /* Read the block */
  171. if (block == 0xFFFFFFFF)
  172. return NULL;
  173. bh = sb_bread(sb, block);
  174. if (!bh) {
  175. udf_err(sb, "read failed, block=%u, location=%u\n",
  176. block, location);
  177. return NULL;
  178. }
  179. tag_p = (struct tag *)(bh->b_data);
  180. *ident = le16_to_cpu(tag_p->tagIdent);
  181. if (location != le32_to_cpu(tag_p->tagLocation)) {
  182. udf_debug("location mismatch block %u, tag %u != %u\n",
  183. block, le32_to_cpu(tag_p->tagLocation), location);
  184. goto error_out;
  185. }
  186. /* Verify the tag checksum */
  187. checksum = udf_tag_checksum(tag_p);
  188. if (checksum != tag_p->tagChecksum) {
  189. udf_err(sb, "tag checksum failed, block %u: 0x%02x != 0x%02x\n",
  190. block, checksum, tag_p->tagChecksum);
  191. goto error_out;
  192. }
  193. /* Verify the tag version */
  194. if (tag_p->descVersion != cpu_to_le16(0x0002U) &&
  195. tag_p->descVersion != cpu_to_le16(0x0003U)) {
  196. udf_err(sb, "tag version 0x%04x != 0x0002 || 0x0003, block %u\n",
  197. le16_to_cpu(tag_p->descVersion), block);
  198. goto error_out;
  199. }
  200. /* Verify the descriptor CRC */
  201. if (le16_to_cpu(tag_p->descCRCLength) + sizeof(struct tag) > sb->s_blocksize ||
  202. le16_to_cpu(tag_p->descCRC) == crc_itu_t(0,
  203. bh->b_data + sizeof(struct tag),
  204. le16_to_cpu(tag_p->descCRCLength)))
  205. return bh;
  206. udf_debug("Crc failure block %u: crc = %u, crclen = %u\n", block,
  207. le16_to_cpu(tag_p->descCRC),
  208. le16_to_cpu(tag_p->descCRCLength));
  209. error_out:
  210. brelse(bh);
  211. return NULL;
  212. }
  213. struct buffer_head *udf_read_ptagged(struct super_block *sb,
  214. struct kernel_lb_addr *loc,
  215. uint32_t offset, uint16_t *ident)
  216. {
  217. return udf_read_tagged(sb, udf_get_lb_pblock(sb, loc, offset),
  218. loc->logicalBlockNum + offset, ident);
  219. }
  220. void udf_update_tag(char *data, int length)
  221. {
  222. struct tag *tptr = (struct tag *)data;
  223. length -= sizeof(struct tag);
  224. tptr->descCRCLength = cpu_to_le16(length);
  225. tptr->descCRC = cpu_to_le16(crc_itu_t(0, data + sizeof(struct tag), length));
  226. tptr->tagChecksum = udf_tag_checksum(tptr);
  227. }
  228. void udf_new_tag(char *data, uint16_t ident, uint16_t version, uint16_t snum,
  229. uint32_t loc, int length)
  230. {
  231. struct tag *tptr = (struct tag *)data;
  232. tptr->tagIdent = cpu_to_le16(ident);
  233. tptr->descVersion = cpu_to_le16(version);
  234. tptr->tagSerialNum = cpu_to_le16(snum);
  235. tptr->tagLocation = cpu_to_le32(loc);
  236. udf_update_tag(data, length);
  237. }
  238. u8 udf_tag_checksum(const struct tag *t)
  239. {
  240. u8 *data = (u8 *)t;
  241. u8 checksum = 0;
  242. int i;
  243. for (i = 0; i < sizeof(struct tag); ++i)
  244. if (i != 4) /* position of checksum */
  245. checksum += data[i];
  246. return checksum;
  247. }