[PATCH] add imacfb documentation and detection
[linux/fpc-iii.git] / fs / udf / misc.c
blobcc8ca3254db11f95d669f8b93321f6ee2c61517d
1 /*
2 * misc.c
4 * PURPOSE
5 * Miscellaneous routines for the OSTA-UDF(tm) filesystem.
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.
13 * (C) 1998 Dave Boynton
14 * (C) 1998-2004 Ben Fennema
15 * (C) 1999-2000 Stelias Computing Inc
17 * HISTORY
19 * 04/19/99 blf partial support for reading/writing specific EA's
22 #include "udfdecl.h"
24 #include <linux/fs.h>
25 #include <linux/string.h>
26 #include <linux/udf_fs.h>
27 #include <linux/buffer_head.h>
29 #include "udf_i.h"
30 #include "udf_sb.h"
32 struct buffer_head *
33 udf_tgetblk(struct super_block *sb, int block)
35 if (UDF_QUERY_FLAG(sb, UDF_FLAG_VARCONV))
36 return sb_getblk(sb, udf_fixed_to_variable(block));
37 else
38 return sb_getblk(sb, block);
41 struct buffer_head *
42 udf_tread(struct super_block *sb, int block)
44 if (UDF_QUERY_FLAG(sb, UDF_FLAG_VARCONV))
45 return sb_bread(sb, udf_fixed_to_variable(block));
46 else
47 return sb_bread(sb, block);
50 struct genericFormat *
51 udf_add_extendedattr(struct inode * inode, uint32_t size, uint32_t type,
52 uint8_t loc)
54 uint8_t *ea = NULL, *ad = NULL;
55 int offset;
56 uint16_t crclen;
57 int i;
59 ea = UDF_I_DATA(inode);
60 if (UDF_I_LENEATTR(inode))
61 ad = UDF_I_DATA(inode) + UDF_I_LENEATTR(inode);
62 else
64 ad = ea;
65 size += sizeof(struct extendedAttrHeaderDesc);
68 offset = inode->i_sb->s_blocksize - udf_file_entry_alloc_offset(inode) -
69 UDF_I_LENALLOC(inode);
71 /* TODO - Check for FreeEASpace */
73 if (loc & 0x01 && offset >= size)
75 struct extendedAttrHeaderDesc *eahd;
76 eahd = (struct extendedAttrHeaderDesc *)ea;
78 if (UDF_I_LENALLOC(inode))
80 memmove(&ad[size], ad, UDF_I_LENALLOC(inode));
83 if (UDF_I_LENEATTR(inode))
85 /* check checksum/crc */
86 if (le16_to_cpu(eahd->descTag.tagIdent) != TAG_IDENT_EAHD ||
87 le32_to_cpu(eahd->descTag.tagLocation) != UDF_I_LOCATION(inode).logicalBlockNum)
89 return NULL;
92 else
94 size -= sizeof(struct extendedAttrHeaderDesc);
95 UDF_I_LENEATTR(inode) += sizeof(struct extendedAttrHeaderDesc);
96 eahd->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EAHD);
97 if (UDF_SB_UDFREV(inode->i_sb) >= 0x0200)
98 eahd->descTag.descVersion = cpu_to_le16(3);
99 else
100 eahd->descTag.descVersion = cpu_to_le16(2);
101 eahd->descTag.tagSerialNum = cpu_to_le16(UDF_SB_SERIALNUM(inode->i_sb));
102 eahd->descTag.tagLocation = cpu_to_le32(UDF_I_LOCATION(inode).logicalBlockNum);
103 eahd->impAttrLocation = cpu_to_le32(0xFFFFFFFF);
104 eahd->appAttrLocation = cpu_to_le32(0xFFFFFFFF);
107 offset = UDF_I_LENEATTR(inode);
108 if (type < 2048)
110 if (le32_to_cpu(eahd->appAttrLocation) < UDF_I_LENEATTR(inode))
112 uint32_t aal = le32_to_cpu(eahd->appAttrLocation);
113 memmove(&ea[offset - aal + size],
114 &ea[aal], offset - aal);
115 offset -= aal;
116 eahd->appAttrLocation = cpu_to_le32(aal + size);
118 if (le32_to_cpu(eahd->impAttrLocation) < UDF_I_LENEATTR(inode))
120 uint32_t ial = le32_to_cpu(eahd->impAttrLocation);
121 memmove(&ea[offset - ial + size],
122 &ea[ial], offset - ial);
123 offset -= ial;
124 eahd->impAttrLocation = cpu_to_le32(ial + size);
127 else if (type < 65536)
129 if (le32_to_cpu(eahd->appAttrLocation) < UDF_I_LENEATTR(inode))
131 uint32_t aal = le32_to_cpu(eahd->appAttrLocation);
132 memmove(&ea[offset - aal + size],
133 &ea[aal], offset - aal);
134 offset -= aal;
135 eahd->appAttrLocation = cpu_to_le32(aal + size);
138 /* rewrite CRC + checksum of eahd */
139 crclen = sizeof(struct extendedAttrHeaderDesc) - sizeof(tag);
140 eahd->descTag.descCRCLength = cpu_to_le16(crclen);
141 eahd->descTag.descCRC = cpu_to_le16(udf_crc((char *)eahd + sizeof(tag), crclen, 0));
142 eahd->descTag.tagChecksum = 0;
143 for (i=0; i<16; i++)
144 if (i != 4)
145 eahd->descTag.tagChecksum += ((uint8_t *)&(eahd->descTag))[i];
146 UDF_I_LENEATTR(inode) += size;
147 return (struct genericFormat *)&ea[offset];
149 if (loc & 0x02)
152 return NULL;
155 struct genericFormat *
156 udf_get_extendedattr(struct inode *inode, uint32_t type, uint8_t subtype)
158 struct genericFormat *gaf;
159 uint8_t *ea = NULL;
160 uint32_t offset;
162 ea = UDF_I_DATA(inode);
164 if (UDF_I_LENEATTR(inode))
166 struct extendedAttrHeaderDesc *eahd;
167 eahd = (struct extendedAttrHeaderDesc *)ea;
169 /* check checksum/crc */
170 if (le16_to_cpu(eahd->descTag.tagIdent) != TAG_IDENT_EAHD ||
171 le32_to_cpu(eahd->descTag.tagLocation) != UDF_I_LOCATION(inode).logicalBlockNum)
173 return NULL;
176 if (type < 2048)
177 offset = sizeof(struct extendedAttrHeaderDesc);
178 else if (type < 65536)
179 offset = le32_to_cpu(eahd->impAttrLocation);
180 else
181 offset = le32_to_cpu(eahd->appAttrLocation);
183 while (offset < UDF_I_LENEATTR(inode))
185 gaf = (struct genericFormat *)&ea[offset];
186 if (le32_to_cpu(gaf->attrType) == type && gaf->attrSubtype == subtype)
187 return gaf;
188 else
189 offset += le32_to_cpu(gaf->attrLength);
192 return NULL;
196 * udf_read_tagged
198 * PURPOSE
199 * Read the first block of a tagged descriptor.
201 * HISTORY
202 * July 1, 1997 - Andrew E. Mileski
203 * Written, tested, and released.
205 struct buffer_head *
206 udf_read_tagged(struct super_block *sb, uint32_t block, uint32_t location, uint16_t *ident)
208 tag *tag_p;
209 struct buffer_head *bh = NULL;
210 register uint8_t checksum;
211 register int i;
213 /* Read the block */
214 if (block == 0xFFFFFFFF)
215 return NULL;
217 bh = udf_tread(sb, block + UDF_SB_SESSION(sb));
218 if (!bh)
220 udf_debug("block=%d, location=%d: read failed\n", block + UDF_SB_SESSION(sb), location);
221 return NULL;
224 tag_p = (tag *)(bh->b_data);
226 *ident = le16_to_cpu(tag_p->tagIdent);
228 if ( location != le32_to_cpu(tag_p->tagLocation) )
230 udf_debug("location mismatch block %u, tag %u != %u\n",
231 block + UDF_SB_SESSION(sb), le32_to_cpu(tag_p->tagLocation), location);
232 goto error_out;
235 /* Verify the tag checksum */
236 checksum = 0U;
237 for (i = 0; i < 4; i++)
238 checksum += (uint8_t)(bh->b_data[i]);
239 for (i = 5; i < 16; i++)
240 checksum += (uint8_t)(bh->b_data[i]);
241 if (checksum != tag_p->tagChecksum) {
242 printk(KERN_ERR "udf: tag checksum failed block %d\n", block);
243 goto error_out;
246 /* Verify the tag version */
247 if (le16_to_cpu(tag_p->descVersion) != 0x0002U &&
248 le16_to_cpu(tag_p->descVersion) != 0x0003U)
250 udf_debug("tag version 0x%04x != 0x0002 || 0x0003 block %d\n",
251 le16_to_cpu(tag_p->descVersion), block);
252 goto error_out;
255 /* Verify the descriptor CRC */
256 if (le16_to_cpu(tag_p->descCRCLength) + sizeof(tag) > sb->s_blocksize ||
257 le16_to_cpu(tag_p->descCRC) == udf_crc(bh->b_data + sizeof(tag),
258 le16_to_cpu(tag_p->descCRCLength), 0))
260 return bh;
262 udf_debug("Crc failure block %d: crc = %d, crclen = %d\n",
263 block + UDF_SB_SESSION(sb), le16_to_cpu(tag_p->descCRC), le16_to_cpu(tag_p->descCRCLength));
265 error_out:
266 brelse(bh);
267 return NULL;
270 struct buffer_head *
271 udf_read_ptagged(struct super_block *sb, kernel_lb_addr loc, uint32_t offset, uint16_t *ident)
273 return udf_read_tagged(sb, udf_get_lb_pblock(sb, loc, offset),
274 loc.logicalBlockNum + offset, ident);
277 void udf_release_data(struct buffer_head *bh)
279 if (bh)
280 brelse(bh);
283 void udf_update_tag(char *data, int length)
285 tag *tptr = (tag *)data;
286 int i;
288 length -= sizeof(tag);
290 tptr->tagChecksum = 0;
291 tptr->descCRCLength = cpu_to_le16(length);
292 tptr->descCRC = cpu_to_le16(udf_crc(data + sizeof(tag), length, 0));
294 for (i=0; i<16; i++)
295 if (i != 4)
296 tptr->tagChecksum += (uint8_t)(data[i]);
299 void udf_new_tag(char *data, uint16_t ident, uint16_t version, uint16_t snum,
300 uint32_t loc, int length)
302 tag *tptr = (tag *)data;
303 tptr->tagIdent = cpu_to_le16(ident);
304 tptr->descVersion = cpu_to_le16(version);
305 tptr->tagSerialNum = cpu_to_le16(snum);
306 tptr->tagLocation = cpu_to_le32(loc);
307 udf_update_tag(data, length);