ia64/xen-unstable

view tools/libfsimage/iso9660/fsys_iso9660.c @ 19648:f0e2df69a8eb

x86 hvm: Allow cross-vendor migration

Intercept #UD and emulate SYSCALL/SYSENTER/SYSEXIT as necessary.

Signed-off-by: Christoph Egger <Christoph.Egger@amd.com>
Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue May 26 15:01:36 2009 +0100 (2009-05-26)
parents 4b882c41c9b9
children
line source
1 /*
2 * ISO 9660 filesystem backend for GRUB (GRand Unified Bootloader)
3 * including Rock Ridge Extensions support
4 *
5 * Copyright (C) 1998, 1999 Kousuke Takai <tak@kmc.kyoto-u.ac.jp>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21 /*
22 * References:
23 * linux/fs/isofs/rock.[ch]
24 * mkisofs-1.11.1/diag/isoinfo.c
25 * mkisofs-1.11.1/iso9660.h
26 * (all are written by Eric Youngdale)
27 *
28 * Modifications by:
29 * Leonid Lisovskiy <lly@pisem.net> 2003
30 */
32 #include <fsimage_grub.h>
33 #include <limits.h>
35 #include "iso9660.h"
37 #define MAXINT INT_MAX
39 /* iso9660 super-block data in memory */
40 struct iso_sb_info {
41 unsigned long vol_sector;
43 };
45 /* iso fs inode data in memory */
46 struct iso_inode_info {
47 unsigned long file_start;
48 };
50 #define ISO_SUPER \
51 ((struct iso_sb_info *)(FSYS_BUF))
52 #define INODE \
53 ((struct iso_inode_info *)(FSYS_BUF+sizeof(struct iso_sb_info)))
54 #define PRIMDESC ((struct iso_primary_descriptor *)(FSYS_BUF + 2048))
55 #define DIRREC ((struct iso_directory_record *)(FSYS_BUF + 4096))
56 #define RRCONT_BUF ((unsigned char *)(FSYS_BUF + 6144))
57 #define NAME_BUF ((unsigned char *)(FSYS_BUF + 8192))
60 #define log2 grub_log2
62 static int
63 iso9660_devread (fsi_file_t *ffi, int sector, int byte_offset, int byte_len, char *buf)
64 {
65 static int read_count = 0, threshold = 2;
66 unsigned short sector_size_lg2 = log2(512 /*buf_geom.sector_size*/);
68 /*
69 * We have to use own devread() function since BIOS return wrong geometry
70 */
71 if (sector < 0)
72 {
73 errnum = ERR_OUTSIDE_PART;
74 return 0;
75 }
76 if (byte_len <= 0)
77 return 1;
79 #if 0
80 sector += (byte_offset >> sector_size_lg2);
81 byte_offset &= (buf_geom.sector_size - 1);
82 asm volatile ("shl%L0 %1,%0"
83 : "=r"(sector)
84 : "Ic"((int8_t)(ISO_SECTOR_BITS - sector_size_lg2)),
85 "0"(sector));
86 #else
87 sector = (sector * 4) + (byte_offset >> sector_size_lg2);
88 byte_offset &= 511;
89 #endif
91 #if !defined(STAGE1_5)
92 if (disk_read_hook && debug)
93 printf ("<%d, %d, %d>", sector, byte_offset, byte_len);
94 #endif /* !STAGE1_5 */
96 read_count += (byte_len >> 9);
97 if ((read_count >> 11) > threshold) {
98 noisy_printf(".");
99 threshold += 2; /* one dot every 2 MB */
100 }
101 return devread(ffi, sector, byte_offset, byte_len, buf);
102 }
104 static int
105 iso9660_mount (fsi_file_t *ffi, const char *options)
106 {
107 unsigned int sector;
109 /*
110 * Because there is no defined slice type ID for ISO-9660 filesystem,
111 * this test will pass only either (1) if entire disk is used, or
112 * (2) if current partition is BSD style sub-partition whose ID is
113 * ISO-9660.
114 */
115 #if 0
116 if ((current_partition != 0xFFFFFF)
117 && !IS_PC_SLICE_TYPE_BSD_WITH_FS(current_slice, FS_ISO9660))
118 return 0;
119 #endif
121 /*
122 * Currently, only FIRST session of MultiSession disks are supported !!!
123 */
124 for (sector = 16 ; sector < 32 ; sector++)
125 {
126 if (!iso9660_devread(ffi, sector, 0, sizeof(*PRIMDESC), (char *)PRIMDESC))
127 break;
128 /* check ISO_VD_PRIMARY and ISO_STANDARD_ID */
129 if (PRIMDESC->type.l == ISO_VD_PRIMARY
130 && !memcmp(PRIMDESC->id, ISO_STANDARD_ID, sizeof(PRIMDESC->id)))
131 {
132 ISO_SUPER->vol_sector = sector;
133 INODE->file_start = 0;
134 #if 0
135 fsmax = PRIMDESC->volume_space_size.l;
136 #endif
137 return 1;
138 }
139 }
141 return 0;
142 }
144 static int
145 iso9660_dir (fsi_file_t *ffi, char *dirname)
146 {
147 struct iso_directory_record *idr;
148 RR_ptr_t rr_ptr;
149 struct rock_ridge *ce_ptr;
150 unsigned int pathlen;
151 int size;
152 unsigned int extent;
153 unsigned char file_type;
154 unsigned int rr_len;
155 unsigned char rr_flag;
157 idr = &PRIMDESC->root_directory_record;
158 INODE->file_start = 0;
160 do
161 {
162 while (*dirname == '/') /* skip leading slashes */
163 dirname++;
164 /* pathlen = strcspn(dirname, "/\n\t "); */
165 for (pathlen = 0 ;
166 dirname[pathlen]
167 && !isspace((uint8_t)dirname[pathlen]) && dirname[pathlen] != '/' ;
168 pathlen++)
169 ;
171 size = idr->size.l;
172 extent = idr->extent.l;
174 while (size > 0)
175 {
176 if (!iso9660_devread(ffi, extent, 0, ISO_SECTOR_SIZE, (char *)DIRREC))
177 {
178 errnum = ERR_FSYS_CORRUPT;
179 return 0;
180 }
181 extent++;
183 idr = (struct iso_directory_record *)DIRREC;
184 for (; idr->length.l > 0;
185 idr = (struct iso_directory_record *)((char *)idr + idr->length.l) )
186 {
187 const char *name = (const char *)idr->name;
188 unsigned int name_len = idr->name_len.l;
190 file_type = (idr->flags.l & 2) ? ISO_DIRECTORY : ISO_REGULAR;
191 if (name_len == 1)
192 {
193 if ((name[0] == 0) || /* self */
194 (name[0] == 1)) /* parent */
195 continue;
196 }
197 if (name_len > 2 && CHECK2(name + name_len - 2, ';', '1'))
198 {
199 name_len -= 2; /* truncate trailing file version */
200 if (name_len > 1 && name[name_len - 1] == '.')
201 name_len--; /* truncate trailing dot */
202 }
204 /*
205 * Parse Rock-Ridge extension
206 */
207 rr_len = (idr->length.l - idr->name_len.l
208 - sizeof(struct iso_directory_record)
209 + sizeof(idr->name));
210 rr_ptr.ptr = ((char *)idr + idr->name_len.l
211 + sizeof(struct iso_directory_record)
212 - sizeof(idr->name));
213 if (rr_ptr.i & 1)
214 rr_ptr.i++, rr_len--;
215 ce_ptr = NULL;
216 rr_flag = RR_FLAG_NM | RR_FLAG_PX /*| RR_FLAG_SL*/;
218 while (rr_len >= 4)
219 {
220 if (rr_ptr.rr->version != 1)
221 {
222 #ifndef STAGE1_5
223 if (debug)
224 printf(
225 "Non-supported version (%d) RockRidge chunk "
226 "`%c%c'\n", rr_ptr.rr->version,
227 rr_ptr.rr->signature & 0xFF,
228 rr_ptr.rr->signature >> 8);
229 #endif
230 }
231 else
232 {
233 switch (rr_ptr.rr->signature)
234 {
235 case RRMAGIC('R', 'R'):
236 if ( rr_ptr.rr->len >= (4+sizeof(struct RR)))
237 rr_flag &= rr_ptr.rr->u.rr.flags.l;
238 break;
239 case RRMAGIC('N', 'M'):
240 name = (const char *)rr_ptr.rr->u.nm.name;
241 name_len = rr_ptr.rr->len - (4+sizeof(struct NM));
242 rr_flag &= ~RR_FLAG_NM;
243 break;
244 case RRMAGIC('P', 'X'):
245 if (rr_ptr.rr->len >= (4+sizeof(struct PX)))
246 {
247 file_type = ((rr_ptr.rr->u.px.mode.l & POSIX_S_IFMT)
248 == POSIX_S_IFREG
249 ? ISO_REGULAR
250 : ((rr_ptr.rr->u.px.mode.l & POSIX_S_IFMT)
251 == POSIX_S_IFDIR
252 ? ISO_DIRECTORY : ISO_OTHER));
253 rr_flag &= ~RR_FLAG_PX;
254 }
255 break;
256 case RRMAGIC('C', 'E'):
257 if (rr_ptr.rr->len >= (4+sizeof(struct CE)))
258 ce_ptr = rr_ptr.rr;
259 break;
260 #if 0 // RockRidge symlinks are not supported yet
261 case RRMAGIC('S', 'L'):
262 {
263 int slen;
264 unsigned char rootflag, prevflag;
265 char *rpnt = NAME_BUF+1024;
266 struct SL_component *slp;
268 slen = rr_ptr.rr->len - (4+1);
269 slp = &rr_ptr.rr->u.sl.link;
270 while (slen > 1)
271 {
272 rootflag = 0;
273 switch (slp->flags.l)
274 {
275 case 0:
276 memcpy(rpnt, slp->text, slp->len);
277 rpnt += slp->len;
278 break;
279 case 4:
280 *rpnt++ = '.';
281 /* fallthru */
282 case 2:
283 *rpnt++ = '.';
284 break;
285 case 8:
286 rootflag = 1;
287 *rpnt++ = '/';
288 break;
289 default:
290 printf("Symlink component flag not implemented (%d)\n",
291 slp->flags.l);
292 slen = 0;
293 break;
294 }
295 slen -= slp->len + 2;
296 prevflag = slp->flags.l;
297 slp = (struct SL_component *) ((char *) slp + slp->len + 2);
299 if (slen < 2)
300 {
301 /*
302 * If there is another SL record, and this component
303 * record isn't continued, then add a slash.
304 */
305 if ((!rootflag) && (rr_ptr.rr->u.sl.flags.l & 1) && !(prevflag & 1))
306 *rpnt++='/';
307 break;
308 }
310 /*
311 * If this component record isn't continued, then append a '/'.
312 */
313 if (!rootflag && !(prevflag & 1))
314 *rpnt++ = '/';
315 }
316 *rpnt++ = '\0';
317 grub_putstr(NAME_BUF+1024);// debug print!
318 }
319 rr_flag &= ~RR_FLAG_SL;
320 break;
321 #endif
322 default:
323 break;
324 }
325 }
326 if (!rr_flag)
327 /*
328 * There is no more extension we expects...
329 */
330 break;
332 rr_len -= rr_ptr.rr->len;
333 rr_ptr.ptr += rr_ptr.rr->len;
334 if (rr_len < 4 && ce_ptr != NULL)
335 {
336 /* preserve name before loading new extent. */
337 if( RRCONT_BUF <= (unsigned char *)name
338 && (unsigned char *)name < RRCONT_BUF + ISO_SECTOR_SIZE )
339 {
340 memcpy(NAME_BUF, name, name_len);
341 name = (const char *)NAME_BUF;
342 }
343 rr_ptr.ptr = (char *)RRCONT_BUF + ce_ptr->u.ce.offset.l;
344 rr_len = ce_ptr->u.ce.size.l;
345 if (!iso9660_devread(ffi, ce_ptr->u.ce.extent.l, 0, ISO_SECTOR_SIZE, (char *)RRCONT_BUF))
346 {
347 errnum = 0; /* this is not fatal. */
348 break;
349 }
350 ce_ptr = NULL;
351 }
352 } /* rr_len >= 4 */
354 filemax = MAXINT;
355 if (name_len >= pathlen
356 && !memcmp(name, dirname, pathlen))
357 {
358 if (dirname[pathlen] == '/' || !print_possibilities)
359 {
360 /*
361 * DIRNAME is directory component of pathname,
362 * or we are to open a file.
363 */
364 if (pathlen == name_len)
365 {
366 if (dirname[pathlen] == '/')
367 {
368 if (file_type != ISO_DIRECTORY)
369 {
370 errnum = ERR_BAD_FILETYPE;
371 return 0;
372 }
373 goto next_dir_level;
374 }
375 if (file_type != ISO_REGULAR)
376 {
377 errnum = ERR_BAD_FILETYPE;
378 return 0;
379 }
380 INODE->file_start = idr->extent.l;
381 filepos = 0;
382 filemax = idr->size.l;
383 return 1;
384 }
385 }
386 else /* Completion */
387 {
388 #ifndef STAGE1_5
389 if (print_possibilities > 0)
390 print_possibilities = -print_possibilities;
391 memcpy(NAME_BUF, name, name_len);
392 NAME_BUF[name_len] = '\0';
393 print_a_completion (NAME_BUF);
394 #endif
395 }
396 }
397 } /* for */
399 size -= ISO_SECTOR_SIZE;
400 } /* size>0 */
402 if (dirname[pathlen] == '/' || print_possibilities >= 0)
403 {
404 errnum = ERR_FILE_NOT_FOUND;
405 return 0;
406 }
408 next_dir_level:
409 dirname += pathlen;
411 } while (*dirname == '/');
413 return 1;
414 }
416 static int
417 iso9660_read (fsi_file_t *ffi, char *buf, int len)
418 {
419 int sector, blkoffset, size, ret;
421 if (INODE->file_start == 0)
422 return 0;
424 ret = 0;
425 blkoffset = filepos & (ISO_SECTOR_SIZE - 1);
426 sector = filepos >> ISO_SECTOR_BITS;
427 while (len > 0)
428 {
429 size = ISO_SECTOR_SIZE - blkoffset;
430 if (size > len)
431 size = len;
433 disk_read_func = disk_read_hook;
435 if (!iso9660_devread(ffi, INODE->file_start + sector, blkoffset, size, buf))
436 return 0;
438 disk_read_func = NULL;
440 len -= size;
441 buf += size;
442 ret += size;
443 filepos += size;
444 sector++;
445 blkoffset = 0;
446 }
448 return ret;
449 }
451 fsi_plugin_ops_t *
452 fsi_init_plugin(int version, fsi_plugin_t *fp, const char **name)
453 {
454 static fsig_plugin_ops_t ops = {
455 FSIMAGE_PLUGIN_VERSION,
456 .fpo_mount = iso9660_mount,
457 .fpo_dir = iso9660_dir,
458 .fpo_read = iso9660_read
459 };
461 *name = "iso9660";
462 return (fsig_init(fp, &ops));
463 }