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