1 // SPDX-License-Identifier: GPL-2.0-only 1 2 /* 3 * misc.c 4 * 5 * PURPOSE 6 * Miscellaneous routines for the OSTA-UD 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/ 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(str 28 uin 29 { 30 uint8_t *ea = NULL, *ad = NULL; 31 int offset; 32 uint16_t crclen; 33 struct udf_inode_info *iinfo = UDF_I(i 34 35 ea = iinfo->i_data; 36 if (iinfo->i_lenEAttr) { 37 ad = iinfo->i_data + iinfo->i_ 38 } else { 39 ad = ea; 40 size += sizeof(struct extended 41 } 42 43 offset = inode->i_sb->s_blocksize - ud 44 iinfo->i_lenAlloc; 45 46 /* TODO - Check for FreeEASpace */ 47 48 if (loc & 0x01 && offset >= size) { 49 struct extendedAttrHeaderDesc 50 eahd = (struct extendedAttrHea 51 52 if (iinfo->i_lenAlloc) 53 memmove(&ad[size], ad, 54 55 if (iinfo->i_lenEAttr) { 56 /* check checksum/crc 57 if (eahd->descTag.tagI 58 cpu_to 59 le32_to_cpu(eahd-> 60 iinfo- 61 return NULL; 62 } else { 63 struct udf_sb_info *sb 64 65 size -= sizeof(struct 66 iinfo->i_lenEAttr += 67 sizeof(struct 68 eahd->descTag.tagIdent 69 if (sbi->s_udfrev >= 0 70 eahd->descTag. 71 else 72 eahd->descTag. 73 eahd->descTag.tagSeria 74 cpu_to 75 eahd->descTag.tagLocat 76 iinfo- 77 eahd->impAttrLocation 78 eahd->appAttrLocation 79 } 80 81 offset = iinfo->i_lenEAttr; 82 if (type < 2048) { 83 if (le32_to_cpu(eahd-> 84 iinfo- 85 uint32_t aal = 86 le32_t 87 memmove(&ea[of 88 &ea[aa 89 offset -= aal; 90 eahd->appAttrL 91 92 } 93 if (le32_to_cpu(eahd-> 94 iinfo- 95 uint32_t ial = 96 le32_t 97 memmove(&ea[of 98 &ea[ia 99 offset -= ial; 100 eahd->impAttrL 101 102 } 103 } else if (type < 65536) { 104 if (le32_to_cpu(eahd-> 105 iinfo- 106 uint32_t aal = 107 le32_t 108 memmove(&ea[of 109 &ea[aa 110 offset -= aal; 111 eahd->appAttrL 112 113 } 114 } 115 /* rewrite CRC + checksum of e 116 crclen = sizeof(struct extende 117 eahd->descTag.descCRCLength = 118 eahd->descTag.descCRC = cpu_to 119 120 eahd->descTag.tagChecksum = ud 121 iinfo->i_lenEAttr += size; 122 return (struct genericFormat * 123 } 124 125 return NULL; 126 } 127 128 struct genericFormat *udf_get_extendedattr(str 129 uin 130 { 131 struct genericFormat *gaf; 132 uint8_t *ea = NULL; 133 uint32_t offset; 134 struct udf_inode_info *iinfo = UDF_I(i 135 136 ea = iinfo->i_data; 137 138 if (iinfo->i_lenEAttr) { 139 struct extendedAttrHeaderDesc 140 eahd = (struct extendedAttrHea 141 142 /* check checksum/crc */ 143 if (eahd->descTag.tagIdent != 144 cpu_to_le16(TA 145 le32_to_cpu(eahd->descTag. 146 iinfo->i_locat 147 return NULL; 148 149 if (type < 2048) 150 offset = sizeof(struct 151 else if (type < 65536) 152 offset = le32_to_cpu(e 153 else 154 offset = le32_to_cpu(e 155 156 while (offset + sizeof(*gaf) < 157 uint32_t attrLength; 158 159 gaf = (struct genericF 160 attrLength = le32_to_c 161 162 /* Detect undersized e 163 if ((attrLength < size 164 (attrLength > (iin 165 break; 166 167 if (le32_to_cpu(gaf->a 168 gaf->a 169 return gaf; 170 else 171 offset += attr 172 } 173 } 174 175 return NULL; 176 } 177 178 /* 179 * udf_read_tagged 180 * 181 * PURPOSE 182 * Read the first block of a tagged descr 183 * 184 * HISTORY 185 * July 1, 1997 - Andrew E. Mileski 186 * Written, tested, and released. 187 */ 188 struct buffer_head *udf_read_tagged(struct sup 189 uint32_t l 190 { 191 struct tag *tag_p; 192 struct buffer_head *bh = NULL; 193 u8 checksum; 194 195 /* Read the block */ 196 if (block == 0xFFFFFFFF) 197 return NULL; 198 199 bh = sb_bread(sb, block); 200 if (!bh) { 201 udf_err(sb, "read failed, bloc 202 block, location); 203 return NULL; 204 } 205 206 tag_p = (struct tag *)(bh->b_data); 207 208 *ident = le16_to_cpu(tag_p->tagIdent); 209 210 if (location != le32_to_cpu(tag_p->tag 211 udf_debug("location mismatch b 212 block, le32_to_cpu(t 213 goto error_out; 214 } 215 216 /* Verify the tag checksum */ 217 checksum = udf_tag_checksum(tag_p); 218 if (checksum != tag_p->tagChecksum) { 219 udf_err(sb, "tag checksum fail 220 block, checksum, tag_p 221 goto error_out; 222 } 223 224 /* Verify the tag version */ 225 if (tag_p->descVersion != cpu_to_le16( 226 tag_p->descVersion != cpu_to_le16( 227 udf_err(sb, "tag version 0x%04 228 le16_to_cpu(tag_p->des 229 goto error_out; 230 } 231 232 /* Verify the descriptor CRC */ 233 if (le16_to_cpu(tag_p->descCRCLength) 234 le16_to_cpu(tag_p->descCRC) == crc 235 bh->b_ 236 le16_t 237 return bh; 238 239 udf_debug("Crc failure block %u: crc = 240 le16_to_cpu(tag_p->descCRC), 241 le16_to_cpu(tag_p->descCRCLe 242 error_out: 243 brelse(bh); 244 return NULL; 245 } 246 247 struct buffer_head *udf_read_ptagged(struct su 248 struct ke 249 uint32_t 250 { 251 return udf_read_tagged(sb, udf_get_lb_ 252 loc->logicalBlo 253 } 254 255 void udf_update_tag(char *data, int length) 256 { 257 struct tag *tptr = (struct tag *)data; 258 length -= sizeof(struct tag); 259 260 tptr->descCRCLength = cpu_to_le16(leng 261 tptr->descCRC = cpu_to_le16(crc_itu_t( 262 tptr->tagChecksum = udf_tag_checksum(t 263 } 264 265 void udf_new_tag(char *data, uint16_t ident, u 266 uint32_t loc, int length) 267 { 268 struct tag *tptr = (struct tag *)data; 269 tptr->tagIdent = cpu_to_le16(ident); 270 tptr->descVersion = cpu_to_le16(versio 271 tptr->tagSerialNum = cpu_to_le16(snum) 272 tptr->tagLocation = cpu_to_le32(loc); 273 udf_update_tag(data, length); 274 } 275 276 u8 udf_tag_checksum(const struct tag *t) 277 { 278 u8 *data = (u8 *)t; 279 u8 checksum = 0; 280 int i; 281 for (i = 0; i < sizeof(struct tag); ++ 282 if (i != 4) /* position of che 283 checksum += data[i]; 284 return checksum; 285 } 286
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.