iso9660.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  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. #include <grub/datetime.h>
  30. GRUB_MOD_LICENSE ("GPLv3+");
  31. #define GRUB_ISO9660_FSTYPE_DIR 0040000
  32. #define GRUB_ISO9660_FSTYPE_REG 0100000
  33. #define GRUB_ISO9660_FSTYPE_SYMLINK 0120000
  34. #define GRUB_ISO9660_FSTYPE_MASK 0170000
  35. #define GRUB_ISO9660_LOG2_BLKSZ 2
  36. #define GRUB_ISO9660_BLKSZ 2048
  37. #define GRUB_ISO9660_RR_DOT 2
  38. #define GRUB_ISO9660_RR_DOTDOT 4
  39. #define GRUB_ISO9660_VOLDESC_BOOT 0
  40. #define GRUB_ISO9660_VOLDESC_PRIMARY 1
  41. #define GRUB_ISO9660_VOLDESC_SUPP 2
  42. #define GRUB_ISO9660_VOLDESC_PART 3
  43. #define GRUB_ISO9660_VOLDESC_END 255
  44. /* The head of a volume descriptor. */
  45. struct grub_iso9660_voldesc
  46. {
  47. grub_uint8_t type;
  48. grub_uint8_t magic[5];
  49. grub_uint8_t version;
  50. } GRUB_PACKED;
  51. struct grub_iso9660_date2
  52. {
  53. grub_uint8_t year;
  54. grub_uint8_t month;
  55. grub_uint8_t day;
  56. grub_uint8_t hour;
  57. grub_uint8_t minute;
  58. grub_uint8_t second;
  59. grub_uint8_t offset;
  60. } GRUB_PACKED;
  61. /* A directory entry. */
  62. struct grub_iso9660_dir
  63. {
  64. grub_uint8_t len;
  65. grub_uint8_t ext_sectors;
  66. grub_uint32_t first_sector;
  67. grub_uint32_t first_sector_be;
  68. grub_uint32_t size;
  69. grub_uint32_t size_be;
  70. struct grub_iso9660_date2 mtime;
  71. grub_uint8_t flags;
  72. grub_uint8_t unused2[6];
  73. #define MAX_NAMELEN 255
  74. grub_uint8_t namelen;
  75. } GRUB_PACKED;
  76. struct grub_iso9660_date
  77. {
  78. grub_uint8_t year[4];
  79. grub_uint8_t month[2];
  80. grub_uint8_t day[2];
  81. grub_uint8_t hour[2];
  82. grub_uint8_t minute[2];
  83. grub_uint8_t second[2];
  84. grub_uint8_t hundredth[2];
  85. grub_uint8_t offset;
  86. } GRUB_PACKED;
  87. /* The primary volume descriptor. Only little endian is used. */
  88. struct grub_iso9660_primary_voldesc
  89. {
  90. struct grub_iso9660_voldesc voldesc;
  91. grub_uint8_t unused1[33];
  92. grub_uint8_t volname[32];
  93. grub_uint8_t unused2[16];
  94. grub_uint8_t escape[32];
  95. grub_uint8_t unused3[12];
  96. grub_uint32_t path_table_size;
  97. grub_uint8_t unused4[4];
  98. grub_uint32_t path_table;
  99. grub_uint8_t unused5[12];
  100. struct grub_iso9660_dir rootdir;
  101. grub_uint8_t unused6[624];
  102. struct grub_iso9660_date created;
  103. struct grub_iso9660_date modified;
  104. } GRUB_PACKED;
  105. /* A single entry in the path table. */
  106. struct grub_iso9660_path
  107. {
  108. grub_uint8_t len;
  109. grub_uint8_t sectors;
  110. grub_uint32_t first_sector;
  111. grub_uint16_t parentdir;
  112. grub_uint8_t name[0];
  113. } GRUB_PACKED;
  114. /* An entry in the System Usage area of the directory entry. */
  115. struct grub_iso9660_susp_entry
  116. {
  117. grub_uint8_t sig[2];
  118. grub_uint8_t len;
  119. grub_uint8_t version;
  120. grub_uint8_t data[0];
  121. } GRUB_PACKED;
  122. /* The CE entry. This is used to describe the next block where data
  123. can be found. */
  124. struct grub_iso9660_susp_ce
  125. {
  126. struct grub_iso9660_susp_entry entry;
  127. grub_uint32_t blk;
  128. grub_uint32_t blk_be;
  129. grub_uint32_t off;
  130. grub_uint32_t off_be;
  131. grub_uint32_t len;
  132. grub_uint32_t len_be;
  133. } GRUB_PACKED;
  134. struct grub_iso9660_data
  135. {
  136. struct grub_iso9660_primary_voldesc voldesc;
  137. grub_disk_t disk;
  138. int rockridge;
  139. int susp_skip;
  140. int joliet;
  141. struct grub_fshelp_node *node;
  142. };
  143. struct grub_fshelp_node
  144. {
  145. struct grub_iso9660_data *data;
  146. grub_size_t have_dirents, alloc_dirents;
  147. int have_symlink;
  148. struct grub_iso9660_dir dirents[8];
  149. char symlink[0];
  150. };
  151. enum
  152. {
  153. FLAG_TYPE_PLAIN = 0,
  154. FLAG_TYPE_DIR = 2,
  155. FLAG_TYPE = 3,
  156. FLAG_MORE_EXTENTS = 0x80
  157. };
  158. static grub_dl_t my_mod;
  159. static grub_err_t
  160. iso9660_to_unixtime (const struct grub_iso9660_date *i, grub_int32_t *nix)
  161. {
  162. struct grub_datetime datetime;
  163. if (! i->year[0] && ! i->year[1]
  164. && ! i->year[2] && ! i->year[3]
  165. && ! i->month[0] && ! i->month[1]
  166. && ! i->day[0] && ! i->day[1]
  167. && ! i->hour[0] && ! i->hour[1]
  168. && ! i->minute[0] && ! i->minute[1]
  169. && ! i->second[0] && ! i->second[1]
  170. && ! i->hundredth[0] && ! i->hundredth[1])
  171. return grub_error (GRUB_ERR_BAD_NUMBER, "empty date");
  172. datetime.year = (i->year[0] - '0') * 1000 + (i->year[1] - '0') * 100
  173. + (i->year[2] - '0') * 10 + (i->year[3] - '0');
  174. datetime.month = (i->month[0] - '0') * 10 + (i->month[1] - '0');
  175. datetime.day = (i->day[0] - '0') * 10 + (i->day[1] - '0');
  176. datetime.hour = (i->hour[0] - '0') * 10 + (i->hour[1] - '0');
  177. datetime.minute = (i->minute[0] - '0') * 10 + (i->minute[1] - '0');
  178. datetime.second = (i->second[0] - '0') * 10 + (i->second[1] - '0');
  179. if (!grub_datetime2unixtime (&datetime, nix))
  180. return grub_error (GRUB_ERR_BAD_NUMBER, "incorrect date");
  181. *nix -= i->offset * 60 * 15;
  182. return GRUB_ERR_NONE;
  183. }
  184. static int
  185. iso9660_to_unixtime2 (const struct grub_iso9660_date2 *i, grub_int32_t *nix)
  186. {
  187. struct grub_datetime datetime;
  188. datetime.year = i->year + 1900;
  189. datetime.month = i->month;
  190. datetime.day = i->day;
  191. datetime.hour = i->hour;
  192. datetime.minute = i->minute;
  193. datetime.second = i->second;
  194. if (!grub_datetime2unixtime (&datetime, nix))
  195. return 0;
  196. *nix -= i->offset * 60 * 15;
  197. return 1;
  198. }
  199. static grub_err_t
  200. read_node (grub_fshelp_node_t node, grub_off_t off, grub_size_t len, char *buf)
  201. {
  202. grub_size_t i = 0;
  203. while (len > 0)
  204. {
  205. grub_size_t toread;
  206. grub_err_t err;
  207. while (i < node->have_dirents
  208. && off >= grub_le_to_cpu32 (node->dirents[i].size))
  209. {
  210. off -= grub_le_to_cpu32 (node->dirents[i].size);
  211. i++;
  212. }
  213. if (i == node->have_dirents)
  214. return grub_error (GRUB_ERR_OUT_OF_RANGE, "read out of range");
  215. toread = grub_le_to_cpu32 (node->dirents[i].size);
  216. if (toread > len)
  217. toread = len;
  218. err = grub_disk_read (node->data->disk,
  219. ((grub_disk_addr_t) grub_le_to_cpu32 (node->dirents[i].first_sector)) << GRUB_ISO9660_LOG2_BLKSZ,
  220. off, toread, buf);
  221. if (err)
  222. return err;
  223. len -= toread;
  224. off += toread;
  225. buf += toread;
  226. }
  227. return GRUB_ERR_NONE;
  228. }
  229. /* Iterate over the susp entries, starting with block SUA_BLOCK on the
  230. offset SUA_POS with a size of SUA_SIZE bytes. Hook is called for
  231. every entry. */
  232. static grub_err_t
  233. grub_iso9660_susp_iterate (grub_fshelp_node_t node, grub_off_t off,
  234. grub_ssize_t sua_size,
  235. grub_err_t (*hook)
  236. (struct grub_iso9660_susp_entry *entry, void *hook_arg),
  237. void *hook_arg)
  238. {
  239. char *sua;
  240. struct grub_iso9660_susp_entry *entry;
  241. grub_err_t err;
  242. if (sua_size <= 0)
  243. return GRUB_ERR_NONE;
  244. sua = grub_malloc (sua_size);
  245. if (!sua)
  246. return grub_errno;
  247. /* Load a part of the System Usage Area. */
  248. err = read_node (node, off, sua_size, sua);
  249. if (err)
  250. return err;
  251. for (entry = (struct grub_iso9660_susp_entry *) sua; (char *) entry < (char *) sua + sua_size - 1 && entry->len > 0;
  252. entry = (struct grub_iso9660_susp_entry *)
  253. ((char *) entry + entry->len))
  254. {
  255. /* The last entry. */
  256. if (grub_strncmp ((char *) entry->sig, "ST", 2) == 0)
  257. break;
  258. /* Additional entries are stored elsewhere. */
  259. if (grub_strncmp ((char *) entry->sig, "CE", 2) == 0)
  260. {
  261. struct grub_iso9660_susp_ce *ce;
  262. grub_disk_addr_t ce_block;
  263. ce = (struct grub_iso9660_susp_ce *) entry;
  264. sua_size = grub_le_to_cpu32 (ce->len);
  265. off = grub_le_to_cpu32 (ce->off);
  266. ce_block = grub_le_to_cpu32 (ce->blk) << GRUB_ISO9660_LOG2_BLKSZ;
  267. grub_free (sua);
  268. sua = grub_malloc (sua_size);
  269. if (!sua)
  270. return grub_errno;
  271. /* Load a part of the System Usage Area. */
  272. err = grub_disk_read (node->data->disk, ce_block, off,
  273. sua_size, sua);
  274. if (err)
  275. return err;
  276. entry = (struct grub_iso9660_susp_entry *) sua;
  277. }
  278. if (hook (entry, hook_arg))
  279. {
  280. grub_free (sua);
  281. return 0;
  282. }
  283. }
  284. grub_free (sua);
  285. return 0;
  286. }
  287. static char *
  288. grub_iso9660_convert_string (grub_uint8_t *us, int len)
  289. {
  290. char *p;
  291. int i;
  292. grub_uint16_t t[MAX_NAMELEN / 2 + 1];
  293. p = grub_malloc (len * GRUB_MAX_UTF8_PER_UTF16 + 1);
  294. if (! p)
  295. return NULL;
  296. for (i=0; i<len; i++)
  297. t[i] = grub_be_to_cpu16 (grub_get_unaligned16 (us + 2 * i));
  298. *grub_utf16_to_utf8 ((grub_uint8_t *) p, t, len) = '\0';
  299. return p;
  300. }
  301. static grub_err_t
  302. susp_iterate_set_rockridge (struct grub_iso9660_susp_entry *susp_entry,
  303. void *_data)
  304. {
  305. struct grub_iso9660_data *data = _data;
  306. /* The "ER" entry is used to detect extensions. The
  307. `IEEE_P1285' extension means Rock ridge. */
  308. if (grub_strncmp ((char *) susp_entry->sig, "ER", 2) == 0)
  309. {
  310. data->rockridge = 1;
  311. return 1;
  312. }
  313. return 0;
  314. }
  315. static grub_err_t
  316. set_rockridge (struct grub_iso9660_data *data)
  317. {
  318. int sua_pos;
  319. int sua_size;
  320. char *sua;
  321. struct grub_iso9660_dir rootdir;
  322. struct grub_iso9660_susp_entry *entry;
  323. data->rockridge = 0;
  324. /* Read the system use area and test it to see if SUSP is
  325. supported. */
  326. if (grub_disk_read (data->disk,
  327. (grub_le_to_cpu32 (data->voldesc.rootdir.first_sector)
  328. << GRUB_ISO9660_LOG2_BLKSZ), 0,
  329. sizeof (rootdir), (char *) &rootdir))
  330. return grub_error (GRUB_ERR_BAD_FS, "not a ISO9660 filesystem");
  331. sua_pos = (sizeof (rootdir) + rootdir.namelen
  332. + (rootdir.namelen % 2) - 1);
  333. sua_size = rootdir.len - sua_pos;
  334. if (!sua_size)
  335. return GRUB_ERR_NONE;
  336. sua = grub_malloc (sua_size);
  337. if (! sua)
  338. return grub_errno;
  339. if (grub_disk_read (data->disk,
  340. (grub_le_to_cpu32 (data->voldesc.rootdir.first_sector)
  341. << GRUB_ISO9660_LOG2_BLKSZ), sua_pos,
  342. sua_size, sua))
  343. {
  344. grub_free (sua);
  345. return grub_error (GRUB_ERR_BAD_FS, "not a ISO9660 filesystem");
  346. }
  347. entry = (struct grub_iso9660_susp_entry *) sua;
  348. /* Test if the SUSP protocol is used on this filesystem. */
  349. if (grub_strncmp ((char *) entry->sig, "SP", 2) == 0)
  350. {
  351. struct grub_fshelp_node rootnode;
  352. rootnode.data = data;
  353. rootnode.alloc_dirents = ARRAY_SIZE (rootnode.dirents);
  354. rootnode.have_dirents = 1;
  355. rootnode.have_symlink = 0;
  356. rootnode.dirents[0] = data->voldesc.rootdir;
  357. /* The 2nd data byte stored how many bytes are skipped every time
  358. to get to the SUA (System Usage Area). */
  359. data->susp_skip = entry->data[2];
  360. entry = (struct grub_iso9660_susp_entry *) ((char *) entry + entry->len);
  361. /* Iterate over the entries in the SUA area to detect
  362. extensions. */
  363. if (grub_iso9660_susp_iterate (&rootnode,
  364. sua_pos, sua_size, susp_iterate_set_rockridge,
  365. data))
  366. {
  367. grub_free (sua);
  368. return grub_errno;
  369. }
  370. }
  371. grub_free (sua);
  372. return GRUB_ERR_NONE;
  373. }
  374. static struct grub_iso9660_data *
  375. grub_iso9660_mount (grub_disk_t disk)
  376. {
  377. struct grub_iso9660_data *data = 0;
  378. struct grub_iso9660_primary_voldesc voldesc;
  379. int block;
  380. data = grub_zalloc (sizeof (struct grub_iso9660_data));
  381. if (! data)
  382. return 0;
  383. data->disk = disk;
  384. block = 16;
  385. do
  386. {
  387. int copy_voldesc = 0;
  388. /* Read the superblock. */
  389. if (grub_disk_read (disk, block << GRUB_ISO9660_LOG2_BLKSZ, 0,
  390. sizeof (struct grub_iso9660_primary_voldesc),
  391. (char *) &voldesc))
  392. {
  393. grub_error (GRUB_ERR_BAD_FS, "not a ISO9660 filesystem");
  394. goto fail;
  395. }
  396. if (grub_strncmp ((char *) voldesc.voldesc.magic, "CD001", 5) != 0)
  397. {
  398. grub_error (GRUB_ERR_BAD_FS, "not a ISO9660 filesystem");
  399. goto fail;
  400. }
  401. if (voldesc.voldesc.type == GRUB_ISO9660_VOLDESC_PRIMARY)
  402. copy_voldesc = 1;
  403. else if (!data->rockridge
  404. && (voldesc.voldesc.type == GRUB_ISO9660_VOLDESC_SUPP)
  405. && (voldesc.escape[0] == 0x25) && (voldesc.escape[1] == 0x2f)
  406. &&
  407. ((voldesc.escape[2] == 0x40) || /* UCS-2 Level 1. */
  408. (voldesc.escape[2] == 0x43) || /* UCS-2 Level 2. */
  409. (voldesc.escape[2] == 0x45))) /* UCS-2 Level 3. */
  410. {
  411. copy_voldesc = 1;
  412. data->joliet = 1;
  413. }
  414. if (copy_voldesc)
  415. {
  416. grub_memcpy((char *) &data->voldesc, (char *) &voldesc,
  417. sizeof (struct grub_iso9660_primary_voldesc));
  418. if (set_rockridge (data))
  419. goto fail;
  420. }
  421. block++;
  422. } while (voldesc.voldesc.type != GRUB_ISO9660_VOLDESC_END);
  423. return data;
  424. fail:
  425. grub_free (data);
  426. return 0;
  427. }
  428. static char *
  429. grub_iso9660_read_symlink (grub_fshelp_node_t node)
  430. {
  431. return node->have_symlink
  432. ? grub_strdup (node->symlink
  433. + (node->have_dirents) * sizeof (node->dirents[0])
  434. - sizeof (node->dirents)) : grub_strdup ("");
  435. }
  436. static grub_off_t
  437. get_node_size (grub_fshelp_node_t node)
  438. {
  439. grub_off_t ret = 0;
  440. grub_size_t i;
  441. for (i = 0; i < node->have_dirents; i++)
  442. ret += grub_le_to_cpu32 (node->dirents[i].size);
  443. return ret;
  444. }
  445. struct iterate_dir_ctx
  446. {
  447. char *filename;
  448. int filename_alloc;
  449. enum grub_fshelp_filetype type;
  450. char *symlink;
  451. int was_continue;
  452. };
  453. /* Extend the symlink. */
  454. static void
  455. add_part (struct iterate_dir_ctx *ctx,
  456. const char *part,
  457. int len2)
  458. {
  459. int size = ctx->symlink ? grub_strlen (ctx->symlink) : 0;
  460. ctx->symlink = grub_realloc (ctx->symlink, size + len2 + 1);
  461. if (! ctx->symlink)
  462. return;
  463. grub_memcpy (ctx->symlink + size, part, len2);
  464. ctx->symlink[size + len2] = 0;
  465. }
  466. static grub_err_t
  467. susp_iterate_dir (struct grub_iso9660_susp_entry *entry,
  468. void *_ctx)
  469. {
  470. struct iterate_dir_ctx *ctx = _ctx;
  471. /* The filename in the rock ridge entry. */
  472. if (grub_strncmp ("NM", (char *) entry->sig, 2) == 0)
  473. {
  474. /* The flags are stored at the data position 0, here the
  475. filename type is stored. */
  476. /* FIXME: Fix this slightly improper cast. */
  477. if (entry->data[0] & GRUB_ISO9660_RR_DOT)
  478. ctx->filename = (char *) ".";
  479. else if (entry->data[0] & GRUB_ISO9660_RR_DOTDOT)
  480. ctx->filename = (char *) "..";
  481. else if (entry->len >= 5)
  482. {
  483. grub_size_t off = 0, csize = 1;
  484. char *old;
  485. csize = entry->len - 5;
  486. old = ctx->filename;
  487. if (ctx->filename_alloc)
  488. {
  489. off = grub_strlen (ctx->filename);
  490. ctx->filename = grub_realloc (ctx->filename, csize + off + 1);
  491. }
  492. else
  493. {
  494. off = 0;
  495. ctx->filename = grub_zalloc (csize + 1);
  496. }
  497. if (!ctx->filename)
  498. {
  499. ctx->filename = old;
  500. return grub_errno;
  501. }
  502. ctx->filename_alloc = 1;
  503. grub_memcpy (ctx->filename + off, (char *) &entry->data[1], csize);
  504. ctx->filename[off + csize] = '\0';
  505. }
  506. }
  507. /* The mode information (st_mode). */
  508. else if (grub_strncmp ((char *) entry->sig, "PX", 2) == 0)
  509. {
  510. /* At position 0 of the PX record the st_mode information is
  511. stored (little-endian). */
  512. grub_uint32_t mode = ((entry->data[0] + (entry->data[1] << 8))
  513. & GRUB_ISO9660_FSTYPE_MASK);
  514. switch (mode)
  515. {
  516. case GRUB_ISO9660_FSTYPE_DIR:
  517. ctx->type = GRUB_FSHELP_DIR;
  518. break;
  519. case GRUB_ISO9660_FSTYPE_REG:
  520. ctx->type = GRUB_FSHELP_REG;
  521. break;
  522. case GRUB_ISO9660_FSTYPE_SYMLINK:
  523. ctx->type = GRUB_FSHELP_SYMLINK;
  524. break;
  525. default:
  526. ctx->type = GRUB_FSHELP_UNKNOWN;
  527. }
  528. }
  529. else if (grub_strncmp ("SL", (char *) entry->sig, 2) == 0)
  530. {
  531. unsigned int pos = 1;
  532. /* The symlink is not stored as a POSIX symlink, translate it. */
  533. while (pos + sizeof (*entry) < entry->len)
  534. {
  535. /* The current position is the `Component Flag'. */
  536. switch (entry->data[pos] & 30)
  537. {
  538. case 0:
  539. {
  540. /* The data on pos + 2 is the actual data, pos + 1
  541. is the length. Both are part of the `Component
  542. Record'. */
  543. if (ctx->symlink && !ctx->was_continue)
  544. add_part (ctx, "/", 1);
  545. add_part (ctx, (char *) &entry->data[pos + 2],
  546. entry->data[pos + 1]);
  547. ctx->was_continue = (entry->data[pos] & 1);
  548. break;
  549. }
  550. case 2:
  551. add_part (ctx, "./", 2);
  552. break;
  553. case 4:
  554. add_part (ctx, "../", 3);
  555. break;
  556. case 8:
  557. add_part (ctx, "/", 1);
  558. break;
  559. }
  560. /* In pos + 1 the length of the `Component Record' is
  561. stored. */
  562. pos += entry->data[pos + 1] + 2;
  563. }
  564. /* Check if `grub_realloc' failed. */
  565. if (grub_errno)
  566. return grub_errno;
  567. }
  568. return 0;
  569. }
  570. static int
  571. grub_iso9660_iterate_dir (grub_fshelp_node_t dir,
  572. grub_fshelp_iterate_dir_hook_t hook, void *hook_data)
  573. {
  574. struct grub_iso9660_dir dirent;
  575. grub_off_t offset = 0;
  576. grub_off_t len;
  577. struct iterate_dir_ctx ctx;
  578. len = get_node_size (dir);
  579. for (; offset < len; offset += dirent.len)
  580. {
  581. ctx.symlink = 0;
  582. ctx.was_continue = 0;
  583. if (read_node (dir, offset, sizeof (dirent), (char *) &dirent))
  584. return 0;
  585. /* The end of the block, skip to the next one. */
  586. if (!dirent.len)
  587. {
  588. offset = (offset / GRUB_ISO9660_BLKSZ + 1) * GRUB_ISO9660_BLKSZ;
  589. continue;
  590. }
  591. {
  592. char name[MAX_NAMELEN + 1];
  593. int nameoffset = offset + sizeof (dirent);
  594. struct grub_fshelp_node *node;
  595. int sua_off = (sizeof (dirent) + dirent.namelen + 1
  596. - (dirent.namelen % 2));
  597. int sua_size = dirent.len - sua_off;
  598. sua_off += offset + dir->data->susp_skip;
  599. ctx.filename = 0;
  600. ctx.filename_alloc = 0;
  601. ctx.type = GRUB_FSHELP_UNKNOWN;
  602. if (dir->data->rockridge
  603. && grub_iso9660_susp_iterate (dir, sua_off, sua_size,
  604. susp_iterate_dir, &ctx))
  605. return 0;
  606. /* Read the name. */
  607. if (read_node (dir, nameoffset, dirent.namelen, (char *) name))
  608. return 0;
  609. node = grub_malloc (sizeof (struct grub_fshelp_node));
  610. if (!node)
  611. return 0;
  612. node->alloc_dirents = ARRAY_SIZE (node->dirents);
  613. node->have_dirents = 1;
  614. /* Setup a new node. */
  615. node->data = dir->data;
  616. node->have_symlink = 0;
  617. /* If the filetype was not stored using rockridge, use
  618. whatever is stored in the iso9660 filesystem. */
  619. if (ctx.type == GRUB_FSHELP_UNKNOWN)
  620. {
  621. if ((dirent.flags & FLAG_TYPE) == FLAG_TYPE_DIR)
  622. ctx.type = GRUB_FSHELP_DIR;
  623. else
  624. ctx.type = GRUB_FSHELP_REG;
  625. }
  626. /* . and .. */
  627. if (!ctx.filename && dirent.namelen == 1 && name[0] == 0)
  628. ctx.filename = (char *) ".";
  629. if (!ctx.filename && dirent.namelen == 1 && name[0] == 1)
  630. ctx.filename = (char *) "..";
  631. /* The filename was not stored in a rock ridge entry. Read it
  632. from the iso9660 filesystem. */
  633. if (!dir->data->joliet && !ctx.filename)
  634. {
  635. char *ptr;
  636. name[dirent.namelen] = '\0';
  637. ctx.filename = grub_strrchr (name, ';');
  638. if (ctx.filename)
  639. *ctx.filename = '\0';
  640. /* ISO9660 names are not case-preserving. */
  641. ctx.type |= GRUB_FSHELP_CASE_INSENSITIVE;
  642. for (ptr = name; *ptr; ptr++)
  643. *ptr = grub_tolower (*ptr);
  644. if (ptr != name && *(ptr - 1) == '.')
  645. *(ptr - 1) = 0;
  646. ctx.filename = name;
  647. }
  648. if (dir->data->joliet && !ctx.filename)
  649. {
  650. char *semicolon;
  651. ctx.filename = grub_iso9660_convert_string
  652. ((grub_uint8_t *) name, dirent.namelen >> 1);
  653. semicolon = grub_strrchr (ctx.filename, ';');
  654. if (semicolon)
  655. *semicolon = '\0';
  656. ctx.filename_alloc = 1;
  657. }
  658. node->dirents[0] = dirent;
  659. while (dirent.flags & FLAG_MORE_EXTENTS)
  660. {
  661. offset += dirent.len;
  662. if (read_node (dir, offset, sizeof (dirent), (char *) &dirent))
  663. {
  664. if (ctx.filename_alloc)
  665. grub_free (ctx.filename);
  666. grub_free (node);
  667. return 0;
  668. }
  669. if (node->have_dirents >= node->alloc_dirents)
  670. {
  671. struct grub_fshelp_node *new_node;
  672. node->alloc_dirents *= 2;
  673. new_node = grub_realloc (node,
  674. sizeof (struct grub_fshelp_node)
  675. + ((node->alloc_dirents
  676. - ARRAY_SIZE (node->dirents))
  677. * sizeof (node->dirents[0])));
  678. if (!new_node)
  679. {
  680. if (ctx.filename_alloc)
  681. grub_free (ctx.filename);
  682. grub_free (node);
  683. return 0;
  684. }
  685. node = new_node;
  686. }
  687. node->dirents[node->have_dirents++] = dirent;
  688. }
  689. if (ctx.symlink)
  690. {
  691. if ((node->alloc_dirents - node->have_dirents)
  692. * sizeof (node->dirents[0]) < grub_strlen (ctx.symlink) + 1)
  693. {
  694. struct grub_fshelp_node *new_node;
  695. new_node = grub_realloc (node,
  696. sizeof (struct grub_fshelp_node)
  697. + ((node->alloc_dirents
  698. - ARRAY_SIZE (node->dirents))
  699. * sizeof (node->dirents[0]))
  700. + grub_strlen (ctx.symlink) + 1);
  701. if (!new_node)
  702. {
  703. if (ctx.filename_alloc)
  704. grub_free (ctx.filename);
  705. grub_free (node);
  706. return 0;
  707. }
  708. node = new_node;
  709. }
  710. node->have_symlink = 1;
  711. grub_strcpy (node->symlink
  712. + node->have_dirents * sizeof (node->dirents[0])
  713. - sizeof (node->dirents), ctx.symlink);
  714. grub_free (ctx.symlink);
  715. ctx.symlink = 0;
  716. ctx.was_continue = 0;
  717. }
  718. if (hook (ctx.filename, ctx.type, node, hook_data))
  719. {
  720. if (ctx.filename_alloc)
  721. grub_free (ctx.filename);
  722. return 1;
  723. }
  724. if (ctx.filename_alloc)
  725. grub_free (ctx.filename);
  726. }
  727. }
  728. return 0;
  729. }
  730. /* Context for grub_iso9660_dir. */
  731. struct grub_iso9660_dir_ctx
  732. {
  733. grub_fs_dir_hook_t hook;
  734. void *hook_data;
  735. };
  736. /* Helper for grub_iso9660_dir. */
  737. static int
  738. grub_iso9660_dir_iter (const char *filename,
  739. enum grub_fshelp_filetype filetype,
  740. grub_fshelp_node_t node, void *data)
  741. {
  742. struct grub_iso9660_dir_ctx *ctx = data;
  743. struct grub_dirhook_info info;
  744. grub_memset (&info, 0, sizeof (info));
  745. info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
  746. info.mtimeset = !!iso9660_to_unixtime2 (&node->dirents[0].mtime, &info.mtime);
  747. grub_free (node);
  748. return ctx->hook (filename, &info, ctx->hook_data);
  749. }
  750. static grub_err_t
  751. grub_iso9660_dir (grub_device_t device, const char *path,
  752. grub_fs_dir_hook_t hook, void *hook_data)
  753. {
  754. struct grub_iso9660_dir_ctx ctx = { hook, hook_data };
  755. struct grub_iso9660_data *data = 0;
  756. struct grub_fshelp_node rootnode;
  757. struct grub_fshelp_node *foundnode;
  758. grub_dl_ref (my_mod);
  759. data = grub_iso9660_mount (device->disk);
  760. if (! data)
  761. goto fail;
  762. rootnode.data = data;
  763. rootnode.alloc_dirents = 0;
  764. rootnode.have_dirents = 1;
  765. rootnode.have_symlink = 0;
  766. rootnode.dirents[0] = data->voldesc.rootdir;
  767. /* Use the fshelp function to traverse the path. */
  768. if (grub_fshelp_find_file (path, &rootnode,
  769. &foundnode,
  770. grub_iso9660_iterate_dir,
  771. grub_iso9660_read_symlink,
  772. GRUB_FSHELP_DIR))
  773. goto fail;
  774. /* List the files in the directory. */
  775. grub_iso9660_iterate_dir (foundnode, grub_iso9660_dir_iter, &ctx);
  776. if (foundnode != &rootnode)
  777. grub_free (foundnode);
  778. fail:
  779. grub_free (data);
  780. grub_dl_unref (my_mod);
  781. return grub_errno;
  782. }
  783. /* Open a file named NAME and initialize FILE. */
  784. static grub_err_t
  785. grub_iso9660_open (struct grub_file *file, const char *name)
  786. {
  787. struct grub_iso9660_data *data;
  788. struct grub_fshelp_node rootnode;
  789. struct grub_fshelp_node *foundnode;
  790. grub_dl_ref (my_mod);
  791. data = grub_iso9660_mount (file->device->disk);
  792. if (!data)
  793. goto fail;
  794. rootnode.data = data;
  795. rootnode.alloc_dirents = 0;
  796. rootnode.have_dirents = 1;
  797. rootnode.have_symlink = 0;
  798. rootnode.dirents[0] = data->voldesc.rootdir;
  799. /* Use the fshelp function to traverse the path. */
  800. if (grub_fshelp_find_file (name, &rootnode,
  801. &foundnode,
  802. grub_iso9660_iterate_dir,
  803. grub_iso9660_read_symlink,
  804. GRUB_FSHELP_REG))
  805. goto fail;
  806. data->node = foundnode;
  807. file->data = data;
  808. file->size = get_node_size (foundnode);
  809. file->offset = 0;
  810. return 0;
  811. fail:
  812. grub_dl_unref (my_mod);
  813. grub_free (data);
  814. return grub_errno;
  815. }
  816. static grub_ssize_t
  817. grub_iso9660_read (grub_file_t file, char *buf, grub_size_t len)
  818. {
  819. struct grub_iso9660_data *data =
  820. (struct grub_iso9660_data *) file->data;
  821. grub_err_t err;
  822. /* XXX: The file is stored in as a single extent. */
  823. data->disk->read_hook = file->read_hook;
  824. data->disk->read_hook_data = file->read_hook_data;
  825. err = read_node (data->node, file->offset, len, buf);
  826. data->disk->read_hook = NULL;
  827. if (err || grub_errno)
  828. return -1;
  829. return len;
  830. }
  831. static grub_err_t
  832. grub_iso9660_close (grub_file_t file)
  833. {
  834. struct grub_iso9660_data *data =
  835. (struct grub_iso9660_data *) file->data;
  836. grub_free (data->node);
  837. grub_free (data);
  838. grub_dl_unref (my_mod);
  839. return GRUB_ERR_NONE;
  840. }
  841. static grub_err_t
  842. grub_iso9660_label (grub_device_t device, char **label)
  843. {
  844. struct grub_iso9660_data *data;
  845. data = grub_iso9660_mount (device->disk);
  846. if (data)
  847. {
  848. if (data->joliet)
  849. *label = grub_iso9660_convert_string (data->voldesc.volname, 16);
  850. else
  851. *label = grub_strndup ((char *) data->voldesc.volname, 32);
  852. if (*label)
  853. {
  854. char *ptr;
  855. for (ptr = *label; *ptr;ptr++);
  856. ptr--;
  857. while (ptr >= *label && *ptr == ' ')
  858. *ptr-- = 0;
  859. }
  860. grub_free (data);
  861. }
  862. else
  863. *label = 0;
  864. return grub_errno;
  865. }
  866. static grub_err_t
  867. grub_iso9660_uuid (grub_device_t device, char **uuid)
  868. {
  869. struct grub_iso9660_data *data;
  870. grub_disk_t disk = device->disk;
  871. grub_dl_ref (my_mod);
  872. data = grub_iso9660_mount (disk);
  873. if (data)
  874. {
  875. if (! data->voldesc.modified.year[0] && ! data->voldesc.modified.year[1]
  876. && ! data->voldesc.modified.year[2] && ! data->voldesc.modified.year[3]
  877. && ! data->voldesc.modified.month[0] && ! data->voldesc.modified.month[1]
  878. && ! data->voldesc.modified.day[0] && ! data->voldesc.modified.day[1]
  879. && ! data->voldesc.modified.hour[0] && ! data->voldesc.modified.hour[1]
  880. && ! data->voldesc.modified.minute[0] && ! data->voldesc.modified.minute[1]
  881. && ! data->voldesc.modified.second[0] && ! data->voldesc.modified.second[1]
  882. && ! data->voldesc.modified.hundredth[0] && ! data->voldesc.modified.hundredth[1])
  883. {
  884. grub_error (GRUB_ERR_BAD_NUMBER, "no creation date in filesystem to generate UUID");
  885. *uuid = NULL;
  886. }
  887. else
  888. {
  889. *uuid = grub_xasprintf ("%c%c%c%c-%c%c-%c%c-%c%c-%c%c-%c%c-%c%c",
  890. data->voldesc.modified.year[0],
  891. data->voldesc.modified.year[1],
  892. data->voldesc.modified.year[2],
  893. data->voldesc.modified.year[3],
  894. data->voldesc.modified.month[0],
  895. data->voldesc.modified.month[1],
  896. data->voldesc.modified.day[0],
  897. data->voldesc.modified.day[1],
  898. data->voldesc.modified.hour[0],
  899. data->voldesc.modified.hour[1],
  900. data->voldesc.modified.minute[0],
  901. data->voldesc.modified.minute[1],
  902. data->voldesc.modified.second[0],
  903. data->voldesc.modified.second[1],
  904. data->voldesc.modified.hundredth[0],
  905. data->voldesc.modified.hundredth[1]);
  906. }
  907. }
  908. else
  909. *uuid = NULL;
  910. grub_dl_unref (my_mod);
  911. grub_free (data);
  912. return grub_errno;
  913. }
  914. /* Get writing time of filesystem. */
  915. static grub_err_t
  916. grub_iso9660_mtime (grub_device_t device, grub_int32_t *timebuf)
  917. {
  918. struct grub_iso9660_data *data;
  919. grub_disk_t disk = device->disk;
  920. grub_err_t err;
  921. grub_dl_ref (my_mod);
  922. data = grub_iso9660_mount (disk);
  923. if (!data)
  924. {
  925. grub_dl_unref (my_mod);
  926. return grub_errno;
  927. }
  928. err = iso9660_to_unixtime (&data->voldesc.modified, timebuf);
  929. grub_dl_unref (my_mod);
  930. grub_free (data);
  931. return err;
  932. }
  933. static struct grub_fs grub_iso9660_fs =
  934. {
  935. .name = "iso9660",
  936. .dir = grub_iso9660_dir,
  937. .open = grub_iso9660_open,
  938. .read = grub_iso9660_read,
  939. .close = grub_iso9660_close,
  940. .label = grub_iso9660_label,
  941. .uuid = grub_iso9660_uuid,
  942. .mtime = grub_iso9660_mtime,
  943. #ifdef GRUB_UTIL
  944. .reserved_first_sector = 1,
  945. .blocklist_install = 1,
  946. #endif
  947. .next = 0
  948. };
  949. GRUB_MOD_INIT(iso9660)
  950. {
  951. grub_fs_register (&grub_iso9660_fs);
  952. my_mod = mod;
  953. }
  954. GRUB_MOD_FINI(iso9660)
  955. {
  956. grub_fs_unregister (&grub_iso9660_fs);
  957. }