iso9660.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  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. /* Iterate over the susp entries, starting with block SUA_BLOCK on the
  140. offset SUA_POS with a size of SUA_SIZE bytes. Hook is called for
  141. every entry. */
  142. static grub_err_t
  143. grub_iso9660_susp_iterate (struct grub_iso9660_data *data,
  144. int sua_block, int sua_pos, int sua_size,
  145. grub_err_t (*hook)
  146. (struct grub_iso9660_susp_entry *entry))
  147. {
  148. char *sua;
  149. struct grub_iso9660_susp_entry *entry;
  150. auto grub_err_t load_sua (void);
  151. /* Load a part of the System Usage Area. */
  152. grub_err_t load_sua (void)
  153. {
  154. sua = grub_malloc (sua_size);
  155. if (!sua)
  156. return grub_errno;
  157. if (grub_disk_read (data->disk, sua_block, sua_pos,
  158. sua_size, sua))
  159. return grub_errno;
  160. entry = (struct grub_iso9660_susp_entry *) sua;
  161. return 0;
  162. }
  163. if (load_sua ())
  164. return grub_errno;
  165. for (; (char *) entry < (char *) sua + sua_size - 1;
  166. entry = (struct grub_iso9660_susp_entry *)
  167. ((char *) entry + entry->len))
  168. {
  169. /* The last entry. */
  170. if (grub_strncmp ((char *) entry->sig, "ST", 2) == 0)
  171. break;
  172. /* Additional entries are stored elsewhere. */
  173. if (grub_strncmp ((char *) entry->sig, "CE", 2) == 0)
  174. {
  175. struct grub_iso9660_susp_ce *ce;
  176. ce = (struct grub_iso9660_susp_ce *) entry;
  177. sua_size = grub_le_to_cpu32 (ce->len);
  178. sua_pos = grub_le_to_cpu32 (ce->off);
  179. sua_block = grub_le_to_cpu32 (ce->blk) << GRUB_ISO9660_LOG2_BLKSZ;
  180. grub_free (sua);
  181. if (load_sua ())
  182. return grub_errno;
  183. }
  184. if (hook (entry))
  185. {
  186. grub_free (sua);
  187. return 0;
  188. }
  189. }
  190. grub_free (sua);
  191. return 0;
  192. }
  193. static char *
  194. grub_iso9660_convert_string (grub_uint16_t *us, int len)
  195. {
  196. char *p;
  197. int i;
  198. p = grub_malloc (len * 4 + 1);
  199. if (! p)
  200. return p;
  201. for (i=0; i<len; i++)
  202. us[i] = grub_be_to_cpu16 (us[i]);
  203. *grub_utf16_to_utf8 ((grub_uint8_t *) p, us, len) = '\0';
  204. return p;
  205. }
  206. static struct grub_iso9660_data *
  207. grub_iso9660_mount (grub_disk_t disk)
  208. {
  209. struct grub_iso9660_data *data = 0;
  210. struct grub_iso9660_dir rootdir;
  211. int sua_pos;
  212. int sua_size;
  213. char *sua;
  214. struct grub_iso9660_susp_entry *entry;
  215. struct grub_iso9660_primary_voldesc voldesc;
  216. int block;
  217. auto grub_err_t susp_iterate (struct grub_iso9660_susp_entry *);
  218. grub_err_t susp_iterate (struct grub_iso9660_susp_entry *susp_entry)
  219. {
  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. data = grub_zalloc (sizeof (struct grub_iso9660_data));
  230. if (! data)
  231. return 0;
  232. data->disk = disk;
  233. block = 16;
  234. do
  235. {
  236. int copy_voldesc = 0;
  237. /* Read the superblock. */
  238. if (grub_disk_read (disk, block << GRUB_ISO9660_LOG2_BLKSZ, 0,
  239. sizeof (struct grub_iso9660_primary_voldesc),
  240. (char *) &voldesc))
  241. {
  242. grub_error (GRUB_ERR_BAD_FS, "not a ISO9660 filesystem");
  243. goto fail;
  244. }
  245. if (grub_strncmp ((char *) voldesc.voldesc.magic, "CD001", 5) != 0)
  246. {
  247. grub_error (GRUB_ERR_BAD_FS, "not a ISO9660 filesystem");
  248. goto fail;
  249. }
  250. if (voldesc.voldesc.type == GRUB_ISO9660_VOLDESC_PRIMARY)
  251. copy_voldesc = 1;
  252. else if ((voldesc.voldesc.type == GRUB_ISO9660_VOLDESC_SUPP) &&
  253. (voldesc.escape[0] == 0x25) && (voldesc.escape[1] == 0x2f) &&
  254. ((voldesc.escape[2] == 0x40) || /* UCS-2 Level 1. */
  255. (voldesc.escape[2] == 0x43) || /* UCS-2 Level 2. */
  256. (voldesc.escape[2] == 0x45))) /* UCS-2 Level 3. */
  257. {
  258. copy_voldesc = 1;
  259. data->joliet = 1;
  260. }
  261. if (copy_voldesc)
  262. grub_memcpy((char *) &data->voldesc, (char *) &voldesc,
  263. sizeof (struct grub_iso9660_primary_voldesc));
  264. block++;
  265. } while (voldesc.voldesc.type != GRUB_ISO9660_VOLDESC_END);
  266. /* Read the system use area and test it to see if SUSP is
  267. supported. */
  268. if (grub_disk_read (disk, (grub_le_to_cpu32 (data->voldesc.rootdir.first_sector)
  269. << GRUB_ISO9660_LOG2_BLKSZ), 0,
  270. sizeof (rootdir), (char *) &rootdir))
  271. {
  272. grub_error (GRUB_ERR_BAD_FS, "not a ISO9660 filesystem");
  273. goto fail;
  274. }
  275. sua_pos = (sizeof (rootdir) + rootdir.namelen
  276. + (rootdir.namelen % 2) - 1);
  277. sua_size = rootdir.len - sua_pos;
  278. sua = grub_malloc (sua_size);
  279. if (! sua)
  280. goto fail;
  281. if (grub_disk_read (disk, (grub_le_to_cpu32 (data->voldesc.rootdir.first_sector)
  282. << GRUB_ISO9660_LOG2_BLKSZ), sua_pos,
  283. sua_size, sua))
  284. {
  285. grub_error (GRUB_ERR_BAD_FS, "not a ISO9660 filesystem");
  286. goto fail;
  287. }
  288. entry = (struct grub_iso9660_susp_entry *) sua;
  289. /* Test if the SUSP protocol is used on this filesystem. */
  290. if (grub_strncmp ((char *) entry->sig, "SP", 2) == 0)
  291. {
  292. /* The 2nd data byte stored how many bytes are skipped every time
  293. to get to the SUA (System Usage Area). */
  294. data->susp_skip = entry->data[2];
  295. entry = (struct grub_iso9660_susp_entry *) ((char *) entry + entry->len);
  296. /* Iterate over the entries in the SUA area to detect
  297. extensions. */
  298. if (grub_iso9660_susp_iterate (data,
  299. (grub_le_to_cpu32 (data->voldesc.rootdir.first_sector)
  300. << GRUB_ISO9660_LOG2_BLKSZ),
  301. sua_pos, sua_size, susp_iterate))
  302. goto fail;
  303. }
  304. return data;
  305. fail:
  306. grub_free (data);
  307. return 0;
  308. }
  309. static char *
  310. grub_iso9660_read_symlink (grub_fshelp_node_t node)
  311. {
  312. struct grub_iso9660_dir dirent;
  313. int sua_off;
  314. int sua_size;
  315. char *symlink = 0;
  316. int addslash = 0;
  317. auto void add_part (const char *part, int len);
  318. auto grub_err_t susp_iterate_sl (struct grub_iso9660_susp_entry *);
  319. /* Extend the symlink. */
  320. void add_part (const char *part, int len)
  321. {
  322. int size = grub_strlen (symlink);
  323. symlink = grub_realloc (symlink, size + len + 1);
  324. if (! symlink)
  325. return;
  326. grub_strncat (symlink, part, len);
  327. }
  328. /* Read in a symlink. */
  329. grub_err_t susp_iterate_sl (struct grub_iso9660_susp_entry *entry)
  330. {
  331. if (grub_strncmp ("SL", (char *) entry->sig, 2) == 0)
  332. {
  333. unsigned int pos = 1;
  334. /* The symlink is not stored as a POSIX symlink, translate it. */
  335. while (pos < grub_le_to_cpu32 (entry->len))
  336. {
  337. if (addslash)
  338. {
  339. add_part ("/", 1);
  340. addslash = 0;
  341. }
  342. /* The current position is the `Component Flag'. */
  343. switch (entry->data[pos] & 30)
  344. {
  345. case 0:
  346. {
  347. /* The data on pos + 2 is the actual data, pos + 1
  348. is the length. Both are part of the `Component
  349. Record'. */
  350. add_part ((char *) &entry->data[pos + 2],
  351. entry->data[pos + 1]);
  352. if ((entry->data[pos] & 1))
  353. addslash = 1;
  354. break;
  355. }
  356. case 2:
  357. add_part ("./", 2);
  358. break;
  359. case 4:
  360. add_part ("../", 3);
  361. break;
  362. case 8:
  363. add_part ("/", 1);
  364. break;
  365. }
  366. /* In pos + 1 the length of the `Component Record' is
  367. stored. */
  368. pos += entry->data[pos + 1] + 2;
  369. }
  370. /* Check if `grub_realloc' failed. */
  371. if (grub_errno)
  372. return grub_errno;
  373. }
  374. return 0;
  375. }
  376. if (grub_disk_read (node->data->disk, node->dir_blk, node->dir_off,
  377. sizeof (dirent), (char *) &dirent))
  378. return 0;
  379. sua_off = (sizeof (dirent) + dirent.namelen + 1 - (dirent.namelen % 2)
  380. + node->data->susp_skip);
  381. sua_size = dirent.len - sua_off;
  382. symlink = grub_malloc (1);
  383. if (!symlink)
  384. return 0;
  385. *symlink = '\0';
  386. if (grub_iso9660_susp_iterate (node->data, node->dir_blk,
  387. node->dir_off + sua_off,
  388. sua_size, susp_iterate_sl))
  389. {
  390. grub_free (symlink);
  391. return 0;
  392. }
  393. return symlink;
  394. }
  395. static int
  396. grub_iso9660_iterate_dir (grub_fshelp_node_t dir,
  397. int NESTED_FUNC_ATTR
  398. (*hook) (const char *filename,
  399. enum grub_fshelp_filetype filetype,
  400. grub_fshelp_node_t node))
  401. {
  402. struct grub_iso9660_dir dirent;
  403. unsigned int offset = 0;
  404. char *filename;
  405. int filename_alloc = 0;
  406. enum grub_fshelp_filetype type;
  407. auto grub_err_t susp_iterate_dir (struct grub_iso9660_susp_entry *);
  408. grub_err_t susp_iterate_dir (struct grub_iso9660_susp_entry *entry)
  409. {
  410. /* The filename in the rock ridge entry. */
  411. if (grub_strncmp ("NM", (char *) entry->sig, 2) == 0)
  412. {
  413. /* The flags are stored at the data position 0, here the
  414. filename type is stored. */
  415. if (entry->data[0] & GRUB_ISO9660_RR_DOT)
  416. filename = ".";
  417. else if (entry->data[0] & GRUB_ISO9660_RR_DOTDOT)
  418. filename = "..";
  419. else
  420. {
  421. int size = 1;
  422. if (filename)
  423. {
  424. size += grub_strlen (filename);
  425. grub_realloc (filename,
  426. grub_strlen (filename)
  427. + entry->len);
  428. }
  429. else
  430. {
  431. size = entry->len - 5;
  432. filename = grub_zalloc (size + 1);
  433. }
  434. filename_alloc = 1;
  435. grub_strncpy (filename, (char *) &entry->data[1], size);
  436. filename[size] = '\0';
  437. }
  438. }
  439. /* The mode information (st_mode). */
  440. else if (grub_strncmp ((char *) entry->sig, "PX", 2) == 0)
  441. {
  442. /* At position 0 of the PX record the st_mode information is
  443. stored (little-endian). */
  444. grub_uint32_t mode = ((entry->data[0] + (entry->data[1] << 8))
  445. & GRUB_ISO9660_FSTYPE_MASK);
  446. switch (mode)
  447. {
  448. case GRUB_ISO9660_FSTYPE_DIR:
  449. type = GRUB_FSHELP_DIR;
  450. break;
  451. case GRUB_ISO9660_FSTYPE_REG:
  452. type = GRUB_FSHELP_REG;
  453. break;
  454. case GRUB_ISO9660_FSTYPE_SYMLINK:
  455. type = GRUB_FSHELP_SYMLINK;
  456. break;
  457. default:
  458. type = GRUB_FSHELP_UNKNOWN;
  459. }
  460. }
  461. return 0;
  462. }
  463. while (offset < dir->size)
  464. {
  465. if (grub_disk_read (dir->data->disk,
  466. (dir->blk << GRUB_ISO9660_LOG2_BLKSZ)
  467. + offset / GRUB_DISK_SECTOR_SIZE,
  468. offset % GRUB_DISK_SECTOR_SIZE,
  469. sizeof (dirent), (char *) &dirent))
  470. return 0;
  471. /* The end of the block, skip to the next one. */
  472. if (!dirent.len)
  473. {
  474. offset = (offset / GRUB_ISO9660_BLKSZ + 1) * GRUB_ISO9660_BLKSZ;
  475. continue;
  476. }
  477. {
  478. char name[dirent.namelen + 1];
  479. int nameoffset = offset + sizeof (dirent);
  480. struct grub_fshelp_node *node;
  481. int sua_off = (sizeof (dirent) + dirent.namelen + 1
  482. - (dirent.namelen % 2));
  483. int sua_size = dirent.len - sua_off;
  484. sua_off += offset + dir->data->susp_skip;
  485. filename = 0;
  486. filename_alloc = 0;
  487. type = GRUB_FSHELP_UNKNOWN;
  488. if (dir->data->rockridge
  489. && grub_iso9660_susp_iterate (dir->data,
  490. (dir->blk << GRUB_ISO9660_LOG2_BLKSZ)
  491. + (sua_off
  492. / GRUB_DISK_SECTOR_SIZE),
  493. sua_off % GRUB_DISK_SECTOR_SIZE,
  494. sua_size, susp_iterate_dir))
  495. return 0;
  496. /* Read the name. */
  497. if (grub_disk_read (dir->data->disk,
  498. (dir->blk << GRUB_ISO9660_LOG2_BLKSZ)
  499. + nameoffset / GRUB_DISK_SECTOR_SIZE,
  500. nameoffset % GRUB_DISK_SECTOR_SIZE,
  501. dirent.namelen, (char *) name))
  502. return 0;
  503. node = grub_malloc (sizeof (struct grub_fshelp_node));
  504. if (!node)
  505. return 0;
  506. /* Setup a new node. */
  507. node->data = dir->data;
  508. node->size = grub_le_to_cpu32 (dirent.size);
  509. node->blk = grub_le_to_cpu32 (dirent.first_sector);
  510. node->dir_blk = ((dir->blk << GRUB_ISO9660_LOG2_BLKSZ)
  511. + offset / GRUB_DISK_SECTOR_SIZE);
  512. node->dir_off = offset % GRUB_DISK_SECTOR_SIZE;
  513. /* If the filetype was not stored using rockridge, use
  514. whatever is stored in the iso9660 filesystem. */
  515. if (type == GRUB_FSHELP_UNKNOWN)
  516. {
  517. if ((dirent.flags & 3) == 2)
  518. type = GRUB_FSHELP_DIR;
  519. else
  520. type = GRUB_FSHELP_REG;
  521. }
  522. /* The filename was not stored in a rock ridge entry. Read it
  523. from the iso9660 filesystem. */
  524. if (!filename)
  525. {
  526. name[dirent.namelen] = '\0';
  527. filename = grub_strrchr (name, ';');
  528. if (filename)
  529. *filename = '\0';
  530. if (dirent.namelen == 1 && name[0] == 0)
  531. filename = ".";
  532. else if (dirent.namelen == 1 && name[0] == 1)
  533. filename = "..";
  534. else
  535. filename = name;
  536. }
  537. if (dir->data->joliet)
  538. {
  539. char *oldname, *semicolon;
  540. oldname = filename;
  541. filename = grub_iso9660_convert_string
  542. ((grub_uint16_t *) oldname, dirent.namelen >> 1);
  543. semicolon = grub_strrchr (filename, ';');
  544. if (semicolon)
  545. *semicolon = '\0';
  546. if (filename_alloc)
  547. grub_free (oldname);
  548. filename_alloc = 1;
  549. }
  550. if (hook (filename, type, node))
  551. {
  552. if (filename_alloc)
  553. grub_free (filename);
  554. return 1;
  555. }
  556. if (filename_alloc)
  557. grub_free (filename);
  558. }
  559. offset += dirent.len;
  560. }
  561. return 0;
  562. }
  563. static grub_err_t
  564. grub_iso9660_dir (grub_device_t device, const char *path,
  565. int (*hook) (const char *filename,
  566. const struct grub_dirhook_info *info))
  567. {
  568. struct grub_iso9660_data *data = 0;
  569. struct grub_fshelp_node rootnode;
  570. struct grub_fshelp_node *foundnode;
  571. auto int NESTED_FUNC_ATTR iterate (const char *filename,
  572. enum grub_fshelp_filetype filetype,
  573. grub_fshelp_node_t node);
  574. int NESTED_FUNC_ATTR iterate (const char *filename,
  575. enum grub_fshelp_filetype filetype,
  576. grub_fshelp_node_t node)
  577. {
  578. struct grub_dirhook_info info;
  579. grub_memset (&info, 0, sizeof (info));
  580. info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
  581. grub_free (node);
  582. return hook (filename, &info);
  583. }
  584. grub_dl_ref (my_mod);
  585. data = grub_iso9660_mount (device->disk);
  586. if (! data)
  587. goto fail;
  588. rootnode.data = data;
  589. rootnode.blk = grub_le_to_cpu32 (data->voldesc.rootdir.first_sector);
  590. rootnode.size = grub_le_to_cpu32 (data->voldesc.rootdir.size);
  591. /* Use the fshelp function to traverse the path. */
  592. if (grub_fshelp_find_file (path, &rootnode,
  593. &foundnode,
  594. grub_iso9660_iterate_dir,
  595. grub_iso9660_read_symlink,
  596. GRUB_FSHELP_DIR))
  597. goto fail;
  598. /* List the files in the directory. */
  599. grub_iso9660_iterate_dir (foundnode, iterate);
  600. if (foundnode != &rootnode)
  601. grub_free (foundnode);
  602. fail:
  603. grub_free (data);
  604. grub_dl_unref (my_mod);
  605. return grub_errno;
  606. }
  607. /* Open a file named NAME and initialize FILE. */
  608. static grub_err_t
  609. grub_iso9660_open (struct grub_file *file, const char *name)
  610. {
  611. struct grub_iso9660_data *data;
  612. struct grub_fshelp_node rootnode;
  613. struct grub_fshelp_node *foundnode;
  614. grub_dl_ref (my_mod);
  615. data = grub_iso9660_mount (file->device->disk);
  616. if (!data)
  617. goto fail;
  618. rootnode.data = data;
  619. rootnode.blk = grub_le_to_cpu32 (data->voldesc.rootdir.first_sector);
  620. rootnode.size = grub_le_to_cpu32 (data->voldesc.rootdir.size);
  621. /* Use the fshelp function to traverse the path. */
  622. if (grub_fshelp_find_file (name, &rootnode,
  623. &foundnode,
  624. grub_iso9660_iterate_dir,
  625. grub_iso9660_read_symlink,
  626. GRUB_FSHELP_REG))
  627. goto fail;
  628. data->first_sector = foundnode->blk;
  629. file->data = data;
  630. file->size = foundnode->size;
  631. file->offset = 0;
  632. return 0;
  633. fail:
  634. grub_dl_unref (my_mod);
  635. grub_free (data);
  636. return grub_errno;
  637. }
  638. static grub_ssize_t
  639. grub_iso9660_read (grub_file_t file, char *buf, grub_size_t len)
  640. {
  641. struct grub_iso9660_data *data =
  642. (struct grub_iso9660_data *) file->data;
  643. /* XXX: The file is stored in as a single extent. */
  644. data->disk->read_hook = file->read_hook;
  645. grub_disk_read (data->disk,
  646. data->first_sector << GRUB_ISO9660_LOG2_BLKSZ,
  647. file->offset,
  648. len, buf);
  649. data->disk->read_hook = NULL;
  650. if (grub_errno)
  651. return -1;
  652. return len;
  653. }
  654. static grub_err_t
  655. grub_iso9660_close (grub_file_t file)
  656. {
  657. grub_free (file->data);
  658. grub_dl_unref (my_mod);
  659. return GRUB_ERR_NONE;
  660. }
  661. static grub_err_t
  662. grub_iso9660_label (grub_device_t device, char **label)
  663. {
  664. struct grub_iso9660_data *data;
  665. data = grub_iso9660_mount (device->disk);
  666. if (data)
  667. {
  668. if (data->joliet)
  669. *label = grub_iso9660_convert_string
  670. ((grub_uint16_t *) &data->voldesc.volname, 16);
  671. else
  672. *label = grub_strndup ((char *) data->voldesc.volname, 32);
  673. grub_free (data);
  674. }
  675. else
  676. *label = 0;
  677. return grub_errno;
  678. }
  679. static grub_err_t
  680. grub_iso9660_uuid (grub_device_t device, char **uuid)
  681. {
  682. struct grub_iso9660_data *data;
  683. grub_disk_t disk = device->disk;
  684. grub_dl_ref (my_mod);
  685. data = grub_iso9660_mount (disk);
  686. if (data)
  687. {
  688. if (! data->voldesc.modified.year[0] && ! data->voldesc.modified.year[1]
  689. && ! data->voldesc.modified.year[2] && ! data->voldesc.modified.year[3]
  690. && ! data->voldesc.modified.month[0] && ! data->voldesc.modified.month[1]
  691. && ! data->voldesc.modified.day[0] && ! data->voldesc.modified.day[1]
  692. && ! data->voldesc.modified.hour[0] && ! data->voldesc.modified.hour[1]
  693. && ! data->voldesc.modified.minute[0] && ! data->voldesc.modified.minute[1]
  694. && ! data->voldesc.modified.second[0] && ! data->voldesc.modified.second[1]
  695. && ! data->voldesc.modified.hundredth[0] && ! data->voldesc.modified.hundredth[1])
  696. {
  697. grub_error (GRUB_ERR_BAD_NUMBER, "no creation date in filesystem to generate UUID");
  698. *uuid = NULL;
  699. }
  700. else
  701. {
  702. *uuid = grub_xasprintf ("%c%c%c%c-%c%c-%c%c-%c%c-%c%c-%c%c-%c%c",
  703. data->voldesc.modified.year[0],
  704. data->voldesc.modified.year[1],
  705. data->voldesc.modified.year[2],
  706. data->voldesc.modified.year[3],
  707. data->voldesc.modified.month[0],
  708. data->voldesc.modified.month[1],
  709. data->voldesc.modified.day[0],
  710. data->voldesc.modified.day[1],
  711. data->voldesc.modified.hour[0],
  712. data->voldesc.modified.hour[1],
  713. data->voldesc.modified.minute[0],
  714. data->voldesc.modified.minute[1],
  715. data->voldesc.modified.second[0],
  716. data->voldesc.modified.second[1],
  717. data->voldesc.modified.hundredth[0],
  718. data->voldesc.modified.hundredth[1]);
  719. }
  720. }
  721. else
  722. *uuid = NULL;
  723. grub_dl_unref (my_mod);
  724. grub_free (data);
  725. return grub_errno;
  726. }
  727. static struct grub_fs grub_iso9660_fs =
  728. {
  729. .name = "iso9660",
  730. .dir = grub_iso9660_dir,
  731. .open = grub_iso9660_open,
  732. .read = grub_iso9660_read,
  733. .close = grub_iso9660_close,
  734. .label = grub_iso9660_label,
  735. .uuid = grub_iso9660_uuid,
  736. .next = 0
  737. };
  738. GRUB_MOD_INIT(iso9660)
  739. {
  740. grub_fs_register (&grub_iso9660_fs);
  741. my_mod = mod;
  742. }
  743. GRUB_MOD_FINI(iso9660)
  744. {
  745. grub_fs_unregister (&grub_iso9660_fs);
  746. }