iso9660.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. /* iso9660.c - iso9660 implementation with extensions:
  2. SUSP, Rock Ridge. */
  3. /*
  4. * GRUB -- GRand Unified Bootloader
  5. * Copyright (C) 2004,2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
  6. *
  7. * GRUB 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 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * GRUB 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 GRUB. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <grub/err.h>
  21. #include <grub/file.h>
  22. #include <grub/mm.h>
  23. #include <grub/misc.h>
  24. #include <grub/disk.h>
  25. #include <grub/dl.h>
  26. #include <grub/types.h>
  27. #include <grub/fshelp.h>
  28. #include <grub/charset.h>
  29. #define GRUB_ISO9660_FSTYPE_DIR 0040000
  30. #define GRUB_ISO9660_FSTYPE_REG 0100000
  31. #define GRUB_ISO9660_FSTYPE_SYMLINK 0120000
  32. #define GRUB_ISO9660_FSTYPE_MASK 0170000
  33. #define GRUB_ISO9660_LOG2_BLKSZ 2
  34. #define GRUB_ISO9660_BLKSZ 2048
  35. #define GRUB_ISO9660_RR_DOT 2
  36. #define GRUB_ISO9660_RR_DOTDOT 4
  37. #define GRUB_ISO9660_VOLDESC_BOOT 0
  38. #define GRUB_ISO9660_VOLDESC_PRIMARY 1
  39. #define GRUB_ISO9660_VOLDESC_SUPP 2
  40. #define GRUB_ISO9660_VOLDESC_PART 3
  41. #define GRUB_ISO9660_VOLDESC_END 255
  42. /* The head of a volume descriptor. */
  43. struct grub_iso9660_voldesc
  44. {
  45. grub_uint8_t type;
  46. grub_uint8_t magic[5];
  47. grub_uint8_t version;
  48. } __attribute__ ((packed));
  49. /* A directory entry. */
  50. struct grub_iso9660_dir
  51. {
  52. grub_uint8_t len;
  53. grub_uint8_t ext_sectors;
  54. grub_uint32_t first_sector;
  55. grub_uint32_t first_sector_be;
  56. grub_uint32_t size;
  57. grub_uint32_t size_be;
  58. grub_uint8_t unused1[7];
  59. grub_uint8_t flags;
  60. grub_uint8_t unused2[6];
  61. grub_uint8_t namelen;
  62. } __attribute__ ((packed));
  63. struct grub_iso9660_date
  64. {
  65. grub_uint8_t year[4];
  66. grub_uint8_t month[2];
  67. grub_uint8_t day[2];
  68. grub_uint8_t hour[2];
  69. grub_uint8_t minute[2];
  70. grub_uint8_t second[2];
  71. grub_uint8_t hundredth[2];
  72. grub_uint8_t offset;
  73. } __attribute__ ((packed));
  74. /* The primary volume descriptor. Only little endian is used. */
  75. struct grub_iso9660_primary_voldesc
  76. {
  77. struct grub_iso9660_voldesc voldesc;
  78. grub_uint8_t unused1[33];
  79. grub_uint8_t volname[32];
  80. grub_uint8_t unused2[16];
  81. grub_uint8_t escape[32];
  82. grub_uint8_t unused3[12];
  83. grub_uint32_t path_table_size;
  84. grub_uint8_t unused4[4];
  85. grub_uint32_t path_table;
  86. grub_uint8_t unused5[12];
  87. struct grub_iso9660_dir rootdir;
  88. grub_uint8_t unused6[624];
  89. struct grub_iso9660_date created;
  90. struct grub_iso9660_date modified;
  91. } __attribute__ ((packed));
  92. /* A single entry in the path table. */
  93. struct grub_iso9660_path
  94. {
  95. grub_uint8_t len;
  96. grub_uint8_t sectors;
  97. grub_uint32_t first_sector;
  98. grub_uint16_t parentdir;
  99. grub_uint8_t name[0];
  100. } __attribute__ ((packed));
  101. /* An entry in the System Usage area of the directory entry. */
  102. struct grub_iso9660_susp_entry
  103. {
  104. grub_uint8_t sig[2];
  105. grub_uint8_t len;
  106. grub_uint8_t version;
  107. grub_uint8_t data[0];
  108. } __attribute__ ((packed));
  109. /* The CE entry. This is used to describe the next block where data
  110. can be found. */
  111. struct grub_iso9660_susp_ce
  112. {
  113. struct grub_iso9660_susp_entry entry;
  114. grub_uint32_t blk;
  115. grub_uint32_t blk_be;
  116. grub_uint32_t off;
  117. grub_uint32_t off_be;
  118. grub_uint32_t len;
  119. grub_uint32_t len_be;
  120. } __attribute__ ((packed));
  121. struct grub_iso9660_data
  122. {
  123. struct grub_iso9660_primary_voldesc voldesc;
  124. grub_disk_t disk;
  125. unsigned int first_sector;
  126. int rockridge;
  127. int susp_skip;
  128. int joliet;
  129. };
  130. struct grub_fshelp_node
  131. {
  132. struct grub_iso9660_data *data;
  133. unsigned int size;
  134. unsigned int blk;
  135. unsigned int dir_blk;
  136. unsigned int dir_off;
  137. };
  138. static grub_dl_t my_mod;
  139. static char *
  140. load_sua (struct grub_iso9660_data *data, int sua_block, int sua_pos,
  141. int sua_size)
  142. {
  143. char *sua;
  144. sua = grub_malloc (sua_size);
  145. if (!sua)
  146. return 0;
  147. if (grub_disk_read (data->disk, sua_block, sua_pos, sua_size, sua))
  148. {
  149. grub_free (sua);
  150. return 0;
  151. }
  152. return sua;
  153. }
  154. /* Iterate over the susp entries, starting with block SUA_BLOCK on the
  155. offset SUA_POS with a size of SUA_SIZE bytes. Hook is called for
  156. every entry. */
  157. static grub_err_t
  158. grub_iso9660_susp_iterate (struct grub_iso9660_data *data,
  159. int sua_block, int sua_pos, int sua_size,
  160. grub_err_t (*hook)
  161. (struct grub_iso9660_susp_entry *entry,
  162. void *closure),
  163. void *closure)
  164. {
  165. char *sua;
  166. struct grub_iso9660_susp_entry *entry;
  167. /* Load a part of the System Usage Area. */
  168. sua = load_sua (data, sua_block, sua_pos, sua_size);
  169. if (!sua)
  170. return grub_errno;
  171. entry = (struct grub_iso9660_susp_entry *) sua;
  172. for (; (char *) entry < (char *) sua + sua_size - 1;
  173. entry = (struct grub_iso9660_susp_entry *)
  174. ((char *) entry + entry->len))
  175. {
  176. /* The last entry. */
  177. if (grub_strncmp ((char *) entry->sig, "ST", 2) == 0)
  178. break;
  179. /* Additional entries are stored elsewhere. */
  180. if (grub_strncmp ((char *) entry->sig, "CE", 2) == 0)
  181. {
  182. struct grub_iso9660_susp_ce *ce;
  183. ce = (struct grub_iso9660_susp_ce *) entry;
  184. sua_size = grub_le_to_cpu32 (ce->len);
  185. sua_pos = grub_le_to_cpu32 (ce->off);
  186. sua_block = grub_le_to_cpu32 (ce->blk) << GRUB_ISO9660_LOG2_BLKSZ;
  187. grub_free (sua);
  188. sua = load_sua (data, sua_block, sua_pos, sua_size);
  189. if (!sua)
  190. return grub_errno;
  191. entry = (struct grub_iso9660_susp_entry *) sua;
  192. }
  193. if (hook (entry, closure))
  194. {
  195. grub_free (sua);
  196. return 0;
  197. }
  198. }
  199. grub_free (sua);
  200. return 0;
  201. }
  202. static char *
  203. grub_iso9660_convert_string (grub_uint16_t *us, int len)
  204. {
  205. char *p;
  206. int i;
  207. p = grub_malloc (len * 4 + 1);
  208. if (! p)
  209. return p;
  210. for (i=0; i<len; i++)
  211. us[i] = grub_be_to_cpu16 (us[i]);
  212. *grub_utf16_to_utf8 ((grub_uint8_t *) p, us, len) = '\0';
  213. return p;
  214. }
  215. static grub_err_t
  216. susp_iterate (struct grub_iso9660_susp_entry *susp_entry,
  217. void *closure)
  218. {
  219. struct grub_iso9660_data *data = closure;
  220. /* The "ER" entry is used to detect extensions. The
  221. `IEEE_P1285' extension means Rock ridge. */
  222. if (grub_strncmp ((char *) susp_entry->sig, "ER", 2) == 0)
  223. {
  224. data->rockridge = 1;
  225. return 1;
  226. }
  227. return 0;
  228. }
  229. static struct grub_iso9660_data *
  230. grub_iso9660_mount (grub_disk_t disk)
  231. {
  232. struct grub_iso9660_data *data = 0;
  233. struct grub_iso9660_dir rootdir;
  234. int sua_pos;
  235. int sua_size;
  236. char *sua;
  237. struct grub_iso9660_susp_entry *entry;
  238. struct grub_iso9660_primary_voldesc voldesc;
  239. int block;
  240. data = grub_zalloc (sizeof (struct grub_iso9660_data));
  241. if (! data)
  242. return 0;
  243. data->disk = disk;
  244. block = 16;
  245. do
  246. {
  247. int copy_voldesc = 0;
  248. /* Read the superblock. */
  249. if (grub_disk_read (disk, block << GRUB_ISO9660_LOG2_BLKSZ, 0,
  250. sizeof (struct grub_iso9660_primary_voldesc),
  251. (char *) &voldesc))
  252. {
  253. grub_error (GRUB_ERR_BAD_FS, "not a ISO9660 filesystem");
  254. goto fail;
  255. }
  256. if (grub_strncmp ((char *) voldesc.voldesc.magic, "CD001", 5) != 0)
  257. {
  258. grub_error (GRUB_ERR_BAD_FS, "not a ISO9660 filesystem");
  259. goto fail;
  260. }
  261. if (voldesc.voldesc.type == GRUB_ISO9660_VOLDESC_PRIMARY)
  262. copy_voldesc = 1;
  263. else if ((voldesc.voldesc.type == GRUB_ISO9660_VOLDESC_SUPP) &&
  264. (voldesc.escape[0] == 0x25) && (voldesc.escape[1] == 0x2f) &&
  265. ((voldesc.escape[2] == 0x40) || /* UCS-2 Level 1. */
  266. (voldesc.escape[2] == 0x43) || /* UCS-2 Level 2. */
  267. (voldesc.escape[2] == 0x45))) /* UCS-2 Level 3. */
  268. {
  269. copy_voldesc = 1;
  270. data->joliet = 1;
  271. }
  272. if (copy_voldesc)
  273. grub_memcpy((char *) &data->voldesc, (char *) &voldesc,
  274. sizeof (struct grub_iso9660_primary_voldesc));
  275. block++;
  276. } while (voldesc.voldesc.type != GRUB_ISO9660_VOLDESC_END);
  277. /* Read the system use area and test it to see if SUSP is
  278. supported. */
  279. if (grub_disk_read (disk,
  280. (grub_le_to_cpu32 (data->voldesc.rootdir.first_sector)
  281. << GRUB_ISO9660_LOG2_BLKSZ), 0,
  282. sizeof (rootdir), (char *) &rootdir))
  283. {
  284. grub_error (GRUB_ERR_BAD_FS, "not a ISO9660 filesystem");
  285. goto fail;
  286. }
  287. sua_pos = (sizeof (rootdir) + rootdir.namelen
  288. + (rootdir.namelen % 2) - 1);
  289. sua_size = rootdir.len - sua_pos;
  290. sua = grub_malloc (sua_size);
  291. if (! sua)
  292. goto fail;
  293. if (grub_disk_read (disk,
  294. (grub_le_to_cpu32 (data->voldesc.rootdir.first_sector)
  295. << GRUB_ISO9660_LOG2_BLKSZ), sua_pos,
  296. sua_size, sua))
  297. {
  298. grub_error (GRUB_ERR_BAD_FS, "not a ISO9660 filesystem");
  299. goto fail;
  300. }
  301. entry = (struct grub_iso9660_susp_entry *) sua;
  302. /* Test if the SUSP protocol is used on this filesystem. */
  303. if (grub_strncmp ((char *) entry->sig, "SP", 2) == 0)
  304. {
  305. /* The 2nd data byte stored how many bytes are skipped every time
  306. to get to the SUA (System Usage Area). */
  307. data->susp_skip = entry->data[2];
  308. entry = (struct grub_iso9660_susp_entry *) ((char *) entry + entry->len);
  309. /* Iterate over the entries in the SUA area to detect
  310. extensions. */
  311. if (grub_iso9660_susp_iterate (data,
  312. (grub_le_to_cpu32 (data->voldesc.rootdir.first_sector)
  313. << GRUB_ISO9660_LOG2_BLKSZ),
  314. sua_pos, sua_size, susp_iterate, data))
  315. goto fail;
  316. }
  317. return data;
  318. fail:
  319. grub_free (data);
  320. return 0;
  321. }
  322. struct grub_iso9660_read_symlink_closure
  323. {
  324. char *symlink;
  325. int addslash;
  326. };
  327. /* Extend the symlink. */
  328. static void
  329. add_part (const char *part, int len,
  330. struct grub_iso9660_read_symlink_closure *c)
  331. {
  332. int size = grub_strlen (c->symlink);
  333. c->symlink = grub_realloc (c->symlink, size + len + 1);
  334. if (! c->symlink)
  335. return;
  336. grub_strncat (c->symlink, part, len);
  337. }
  338. /* Read in a symlink. */
  339. static grub_err_t
  340. susp_iterate_sl (struct grub_iso9660_susp_entry *entry, void *closure)
  341. {
  342. struct grub_iso9660_read_symlink_closure *c = closure;
  343. if (grub_strncmp ("SL", (char *) entry->sig, 2) == 0)
  344. {
  345. unsigned int pos = 1;
  346. /* The symlink is not stored as a POSIX symlink, translate it. */
  347. while (pos < grub_le_to_cpu32 (entry->len))
  348. {
  349. if (c->addslash)
  350. {
  351. add_part ("/", 1, c);
  352. c->addslash = 0;
  353. }
  354. /* The current position is the `Component Flag'. */
  355. switch (entry->data[pos] & 30)
  356. {
  357. case 0:
  358. {
  359. /* The data on pos + 2 is the actual data, pos + 1
  360. is the length. Both are part of the `Component
  361. Record'. */
  362. add_part ((char *) &entry->data[pos + 2],
  363. entry->data[pos + 1], c);
  364. if ((entry->data[pos] & 1))
  365. c->addslash = 1;
  366. break;
  367. }
  368. case 2:
  369. add_part ("./", 2, c);
  370. break;
  371. case 4:
  372. add_part ("../", 3, c);
  373. break;
  374. case 8:
  375. add_part ("/", 1, c);
  376. break;
  377. }
  378. /* In pos + 1 the length of the `Component Record' is
  379. stored. */
  380. pos += entry->data[pos + 1] + 2;
  381. }
  382. /* Check if `grub_realloc' failed. */
  383. if (grub_errno)
  384. return grub_errno;
  385. }
  386. return 0;
  387. }
  388. static char *
  389. grub_iso9660_read_symlink (grub_fshelp_node_t node)
  390. {
  391. struct grub_iso9660_dir dirent;
  392. int sua_off;
  393. int sua_size;
  394. struct grub_iso9660_read_symlink_closure c;
  395. if (grub_disk_read (node->data->disk, node->dir_blk, node->dir_off,
  396. sizeof (dirent), (char *) &dirent))
  397. return 0;
  398. sua_off = (sizeof (dirent) + dirent.namelen + 1 - (dirent.namelen % 2)
  399. + node->data->susp_skip);
  400. sua_size = dirent.len - sua_off;
  401. c.symlink = grub_malloc (1);
  402. if (!c.symlink)
  403. return 0;
  404. *c.symlink = '\0';
  405. c.addslash = 0;
  406. if (grub_iso9660_susp_iterate (node->data, node->dir_blk,
  407. node->dir_off + sua_off,
  408. sua_size, susp_iterate_sl, &c))
  409. {
  410. grub_free (c.symlink);
  411. return 0;
  412. }
  413. return c.symlink;
  414. }
  415. struct grub_iso9660_iterate_dir_closure
  416. {
  417. char **filename;
  418. int filename_alloc;
  419. enum grub_fshelp_filetype type;
  420. };
  421. static grub_err_t
  422. susp_iterate_dir (struct grub_iso9660_susp_entry *entry, void *closure)
  423. {
  424. struct grub_iso9660_iterate_dir_closure *c = closure;
  425. char *filename = *(c->filename);
  426. /* The filename in the rock ridge entry. */
  427. if (grub_strncmp ("NM", (char *) entry->sig, 2) == 0)
  428. {
  429. /* The flags are stored at the data position 0, here the
  430. filename type is stored. */
  431. if (entry->data[0] & GRUB_ISO9660_RR_DOT)
  432. filename = ".";
  433. else if (entry->data[0] & GRUB_ISO9660_RR_DOTDOT)
  434. filename = "..";
  435. else
  436. {
  437. int size = 1;
  438. if (filename)
  439. {
  440. size += grub_strlen (filename);
  441. grub_realloc (filename,
  442. grub_strlen (filename)
  443. + entry->len);
  444. }
  445. else
  446. {
  447. size = entry->len - 5;
  448. filename = grub_zalloc (size + 1);
  449. }
  450. c->filename_alloc = 1;
  451. grub_strncpy (filename, (char *) &entry->data[1], size);
  452. filename[size] = '\0';
  453. }
  454. }
  455. /* The mode information (st_mode). */
  456. else if (grub_strncmp ((char *) entry->sig, "PX", 2) == 0)
  457. {
  458. /* At position 0 of the PX record the st_mode information is
  459. stored (little-endian). */
  460. grub_uint32_t mode = ((entry->data[0] + (entry->data[1] << 8))
  461. & GRUB_ISO9660_FSTYPE_MASK);
  462. switch (mode)
  463. {
  464. case GRUB_ISO9660_FSTYPE_DIR:
  465. c->type = GRUB_FSHELP_DIR;
  466. break;
  467. case GRUB_ISO9660_FSTYPE_REG:
  468. c->type = GRUB_FSHELP_REG;
  469. break;
  470. case GRUB_ISO9660_FSTYPE_SYMLINK:
  471. c->type = GRUB_FSHELP_SYMLINK;
  472. break;
  473. default:
  474. c->type = GRUB_FSHELP_UNKNOWN;
  475. }
  476. }
  477. *(c->filename) = filename;
  478. return 0;
  479. }
  480. static int
  481. grub_iso9660_iterate_dir (grub_fshelp_node_t dir,
  482. int (*hook) (const char *filename,
  483. enum grub_fshelp_filetype filetype,
  484. grub_fshelp_node_t node,
  485. void *closure),
  486. void *closure)
  487. {
  488. struct grub_iso9660_dir dirent;
  489. unsigned int offset = 0;
  490. char *filename;
  491. while (offset < dir->size)
  492. {
  493. if (grub_disk_read (dir->data->disk,
  494. (dir->blk << GRUB_ISO9660_LOG2_BLKSZ)
  495. + offset / GRUB_DISK_SECTOR_SIZE,
  496. offset % GRUB_DISK_SECTOR_SIZE,
  497. sizeof (dirent), (char *) &dirent))
  498. return 0;
  499. /* The end of the block, skip to the next one. */
  500. if (!dirent.len)
  501. {
  502. offset = (offset / GRUB_ISO9660_BLKSZ + 1) * GRUB_ISO9660_BLKSZ;
  503. continue;
  504. }
  505. {
  506. char name[dirent.namelen + 1];
  507. int nameoffset = offset + sizeof (dirent);
  508. struct grub_fshelp_node *node;
  509. int sua_off = (sizeof (dirent) + dirent.namelen + 1
  510. - (dirent.namelen % 2));
  511. int sua_size = dirent.len - sua_off;
  512. struct grub_iso9660_iterate_dir_closure c;
  513. sua_off += offset + dir->data->susp_skip;
  514. filename = 0;
  515. c.filename = &filename;
  516. c.filename_alloc = 0;
  517. c.type = GRUB_FSHELP_UNKNOWN;
  518. if (dir->data->rockridge
  519. && grub_iso9660_susp_iterate (dir->data,
  520. (dir->blk << GRUB_ISO9660_LOG2_BLKSZ)
  521. + (sua_off
  522. / GRUB_DISK_SECTOR_SIZE),
  523. sua_off % GRUB_DISK_SECTOR_SIZE,
  524. sua_size, susp_iterate_dir, &c))
  525. return 0;
  526. /* Read the name. */
  527. if (grub_disk_read (dir->data->disk,
  528. (dir->blk << GRUB_ISO9660_LOG2_BLKSZ)
  529. + nameoffset / GRUB_DISK_SECTOR_SIZE,
  530. nameoffset % GRUB_DISK_SECTOR_SIZE,
  531. dirent.namelen, (char *) name))
  532. return 0;
  533. node = grub_malloc (sizeof (struct grub_fshelp_node));
  534. if (!node)
  535. return 0;
  536. /* Setup a new node. */
  537. node->data = dir->data;
  538. node->size = grub_le_to_cpu32 (dirent.size);
  539. node->blk = grub_le_to_cpu32 (dirent.first_sector);
  540. node->dir_blk = ((dir->blk << GRUB_ISO9660_LOG2_BLKSZ)
  541. + offset / GRUB_DISK_SECTOR_SIZE);
  542. node->dir_off = offset % GRUB_DISK_SECTOR_SIZE;
  543. /* If the filetype was not stored using rockridge, use
  544. whatever is stored in the iso9660 filesystem. */
  545. if (c.type == GRUB_FSHELP_UNKNOWN)
  546. {
  547. if ((dirent.flags & 3) == 2)
  548. c.type = GRUB_FSHELP_DIR;
  549. else
  550. c.type = GRUB_FSHELP_REG;
  551. }
  552. /* The filename was not stored in a rock ridge entry. Read it
  553. from the iso9660 filesystem. */
  554. if (!filename)
  555. {
  556. name[dirent.namelen] = '\0';
  557. filename = grub_strrchr (name, ';');
  558. if (filename)
  559. *filename = '\0';
  560. if (dirent.namelen == 1 && name[0] == 0)
  561. filename = ".";
  562. else if (dirent.namelen == 1 && name[0] == 1)
  563. filename = "..";
  564. else
  565. filename = name;
  566. }
  567. if (dir->data->joliet)
  568. {
  569. char *oldname, *semicolon;
  570. oldname = filename;
  571. filename = grub_iso9660_convert_string
  572. ((grub_uint16_t *) oldname, dirent.namelen >> 1);
  573. semicolon = grub_strrchr (filename, ';');
  574. if (semicolon)
  575. *semicolon = '\0';
  576. if (c.filename_alloc)
  577. grub_free (oldname);
  578. c.filename_alloc = 1;
  579. }
  580. if (hook (filename, c.type, node, closure))
  581. {
  582. if (c.filename_alloc)
  583. grub_free (filename);
  584. return 1;
  585. }
  586. if (c.filename_alloc)
  587. grub_free (filename);
  588. }
  589. offset += dirent.len;
  590. }
  591. return 0;
  592. }
  593. struct grub_iso9660_dir_closure
  594. {
  595. int (*hook) (const char *filename,
  596. const struct grub_dirhook_info *info,
  597. void *closure);
  598. void *closure;
  599. };
  600. static int
  601. iterate (const char *filename,
  602. enum grub_fshelp_filetype filetype,
  603. grub_fshelp_node_t node, void *closure)
  604. {
  605. struct grub_iso9660_dir_closure *c = closure;
  606. struct grub_dirhook_info info;
  607. grub_memset (&info, 0, sizeof (info));
  608. info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
  609. grub_free (node);
  610. return c->hook (filename, &info, c->closure);
  611. }
  612. static grub_err_t
  613. grub_iso9660_dir (grub_device_t device, const char *path,
  614. int (*hook) (const char *filename,
  615. const struct grub_dirhook_info *info,
  616. void *closure),
  617. void *closure)
  618. {
  619. struct grub_iso9660_data *data = 0;
  620. struct grub_fshelp_node rootnode;
  621. struct grub_fshelp_node *foundnode;
  622. struct grub_iso9660_dir_closure c;
  623. grub_dl_ref (my_mod);
  624. data = grub_iso9660_mount (device->disk);
  625. if (! data)
  626. goto fail;
  627. rootnode.data = data;
  628. rootnode.blk = grub_le_to_cpu32 (data->voldesc.rootdir.first_sector);
  629. rootnode.size = grub_le_to_cpu32 (data->voldesc.rootdir.size);
  630. /* Use the fshelp function to traverse the path. */
  631. if (grub_fshelp_find_file (path, &rootnode,
  632. &foundnode,
  633. grub_iso9660_iterate_dir, 0,
  634. grub_iso9660_read_symlink,
  635. GRUB_FSHELP_DIR))
  636. goto fail;
  637. c.hook = hook;
  638. c.closure = closure;
  639. /* List the files in the directory. */
  640. grub_iso9660_iterate_dir (foundnode, iterate, &c);
  641. if (foundnode != &rootnode)
  642. grub_free (foundnode);
  643. fail:
  644. grub_free (data);
  645. grub_dl_unref (my_mod);
  646. return grub_errno;
  647. }
  648. /* Open a file named NAME and initialize FILE. */
  649. static grub_err_t
  650. grub_iso9660_open (struct grub_file *file, const char *name)
  651. {
  652. struct grub_iso9660_data *data;
  653. struct grub_fshelp_node rootnode;
  654. struct grub_fshelp_node *foundnode;
  655. grub_dl_ref (my_mod);
  656. data = grub_iso9660_mount (file->device->disk);
  657. if (!data)
  658. goto fail;
  659. rootnode.data = data;
  660. rootnode.blk = grub_le_to_cpu32 (data->voldesc.rootdir.first_sector);
  661. rootnode.size = grub_le_to_cpu32 (data->voldesc.rootdir.size);
  662. /* Use the fshelp function to traverse the path. */
  663. if (grub_fshelp_find_file (name, &rootnode,
  664. &foundnode,
  665. grub_iso9660_iterate_dir, 0,
  666. grub_iso9660_read_symlink,
  667. GRUB_FSHELP_REG))
  668. goto fail;
  669. data->first_sector = foundnode->blk;
  670. file->data = data;
  671. file->size = foundnode->size;
  672. file->offset = 0;
  673. return 0;
  674. fail:
  675. grub_dl_unref (my_mod);
  676. grub_free (data);
  677. return grub_errno;
  678. }
  679. static grub_ssize_t
  680. grub_iso9660_read (grub_file_t file, char *buf, grub_size_t len)
  681. {
  682. struct grub_iso9660_data *data =
  683. (struct grub_iso9660_data *) file->data;
  684. /* XXX: The file is stored in as a single extent. */
  685. data->disk->read_hook = file->read_hook;
  686. data->disk->closure = file->closure;
  687. grub_disk_read_ex (data->disk,
  688. data->first_sector << GRUB_ISO9660_LOG2_BLKSZ,
  689. file->offset,
  690. len, buf, file->flags);
  691. data->disk->read_hook = NULL;
  692. if (grub_errno)
  693. return -1;
  694. return len;
  695. }
  696. static grub_err_t
  697. grub_iso9660_close (grub_file_t file)
  698. {
  699. grub_free (file->data);
  700. grub_dl_unref (my_mod);
  701. return GRUB_ERR_NONE;
  702. }
  703. static grub_err_t
  704. grub_iso9660_label (grub_device_t device, char **label)
  705. {
  706. struct grub_iso9660_data *data;
  707. data = grub_iso9660_mount (device->disk);
  708. if (data)
  709. {
  710. if (data->joliet)
  711. *label = grub_iso9660_convert_string
  712. ((grub_uint16_t *) &data->voldesc.volname, 16);
  713. else
  714. *label = grub_strndup ((char *) data->voldesc.volname, 32);
  715. grub_free (data);
  716. }
  717. else
  718. *label = 0;
  719. return grub_errno;
  720. }
  721. static grub_err_t
  722. grub_iso9660_uuid (grub_device_t device, char **uuid)
  723. {
  724. struct grub_iso9660_data *data;
  725. grub_disk_t disk = device->disk;
  726. grub_dl_ref (my_mod);
  727. data = grub_iso9660_mount (disk);
  728. if (data)
  729. {
  730. if (! data->voldesc.modified.year[0] && ! data->voldesc.modified.year[1]
  731. && ! data->voldesc.modified.year[2] && ! data->voldesc.modified.year[3]
  732. && ! data->voldesc.modified.month[0] && ! data->voldesc.modified.month[1]
  733. && ! data->voldesc.modified.day[0] && ! data->voldesc.modified.day[1]
  734. && ! data->voldesc.modified.hour[0] && ! data->voldesc.modified.hour[1]
  735. && ! data->voldesc.modified.minute[0] && ! data->voldesc.modified.minute[1]
  736. && ! data->voldesc.modified.second[0] && ! data->voldesc.modified.second[1]
  737. && ! data->voldesc.modified.hundredth[0] && ! data->voldesc.modified.hundredth[1])
  738. {
  739. grub_error (GRUB_ERR_BAD_NUMBER, "no creation date in filesystem to generate UUID");
  740. *uuid = NULL;
  741. }
  742. else
  743. {
  744. *uuid = grub_xasprintf ("%c%c%c%c-%c%c-%c%c-%c%c-%c%c-%c%c-%c%c",
  745. data->voldesc.modified.year[0],
  746. data->voldesc.modified.year[1],
  747. data->voldesc.modified.year[2],
  748. data->voldesc.modified.year[3],
  749. data->voldesc.modified.month[0],
  750. data->voldesc.modified.month[1],
  751. data->voldesc.modified.day[0],
  752. data->voldesc.modified.day[1],
  753. data->voldesc.modified.hour[0],
  754. data->voldesc.modified.hour[1],
  755. data->voldesc.modified.minute[0],
  756. data->voldesc.modified.minute[1],
  757. data->voldesc.modified.second[0],
  758. data->voldesc.modified.second[1],
  759. data->voldesc.modified.hundredth[0],
  760. data->voldesc.modified.hundredth[1]);
  761. }
  762. }
  763. else
  764. *uuid = NULL;
  765. grub_dl_unref (my_mod);
  766. grub_free (data);
  767. return grub_errno;
  768. }
  769. static struct grub_fs grub_iso9660_fs =
  770. {
  771. .name = "iso9660",
  772. .dir = grub_iso9660_dir,
  773. .open = grub_iso9660_open,
  774. .read = grub_iso9660_read,
  775. .close = grub_iso9660_close,
  776. .label = grub_iso9660_label,
  777. .uuid = grub_iso9660_uuid,
  778. .next = 0
  779. };
  780. GRUB_MOD_INIT(iso9660)
  781. {
  782. grub_fs_register (&grub_iso9660_fs);
  783. my_mod = mod;
  784. }
  785. GRUB_MOD_FINI(iso9660)
  786. {
  787. grub_fs_unregister (&grub_iso9660_fs);
  788. }