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