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