udf.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  1. /* udf.c - Universal Disk Format filesystem. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2008,2009 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/err.h>
  20. #include <grub/file.h>
  21. #include <grub/mm.h>
  22. #include <grub/misc.h>
  23. #include <grub/disk.h>
  24. #include <grub/dl.h>
  25. #include <grub/types.h>
  26. #include <grub/fshelp.h>
  27. #include <grub/charset.h>
  28. #define GRUB_UDF_MAX_PDS 2
  29. #define GRUB_UDF_MAX_PMS 6
  30. #define U16 grub_le_to_cpu16
  31. #define U32 grub_le_to_cpu32
  32. #define U64 grub_le_to_cpu64
  33. #define GRUB_UDF_LOG2_BLKSZ 2
  34. #define GRUB_UDF_BLKSZ 2048
  35. #define GRUB_UDF_TAG_IDENT_PVD 0x0001
  36. #define GRUB_UDF_TAG_IDENT_AVDP 0x0002
  37. #define GRUB_UDF_TAG_IDENT_VDP 0x0003
  38. #define GRUB_UDF_TAG_IDENT_IUVD 0x0004
  39. #define GRUB_UDF_TAG_IDENT_PD 0x0005
  40. #define GRUB_UDF_TAG_IDENT_LVD 0x0006
  41. #define GRUB_UDF_TAG_IDENT_USD 0x0007
  42. #define GRUB_UDF_TAG_IDENT_TD 0x0008
  43. #define GRUB_UDF_TAG_IDENT_LVID 0x0009
  44. #define GRUB_UDF_TAG_IDENT_FSD 0x0100
  45. #define GRUB_UDF_TAG_IDENT_FID 0x0101
  46. #define GRUB_UDF_TAG_IDENT_AED 0x0102
  47. #define GRUB_UDF_TAG_IDENT_IE 0x0103
  48. #define GRUB_UDF_TAG_IDENT_TE 0x0104
  49. #define GRUB_UDF_TAG_IDENT_FE 0x0105
  50. #define GRUB_UDF_TAG_IDENT_EAHD 0x0106
  51. #define GRUB_UDF_TAG_IDENT_USE 0x0107
  52. #define GRUB_UDF_TAG_IDENT_SBD 0x0108
  53. #define GRUB_UDF_TAG_IDENT_PIE 0x0109
  54. #define GRUB_UDF_TAG_IDENT_EFE 0x010A
  55. #define GRUB_UDF_ICBTAG_TYPE_UNDEF 0x00
  56. #define GRUB_UDF_ICBTAG_TYPE_USE 0x01
  57. #define GRUB_UDF_ICBTAG_TYPE_PIE 0x02
  58. #define GRUB_UDF_ICBTAG_TYPE_IE 0x03
  59. #define GRUB_UDF_ICBTAG_TYPE_DIRECTORY 0x04
  60. #define GRUB_UDF_ICBTAG_TYPE_REGULAR 0x05
  61. #define GRUB_UDF_ICBTAG_TYPE_BLOCK 0x06
  62. #define GRUB_UDF_ICBTAG_TYPE_CHAR 0x07
  63. #define GRUB_UDF_ICBTAG_TYPE_EA 0x08
  64. #define GRUB_UDF_ICBTAG_TYPE_FIFO 0x09
  65. #define GRUB_UDF_ICBTAG_TYPE_SOCKET 0x0A
  66. #define GRUB_UDF_ICBTAG_TYPE_TE 0x0B
  67. #define GRUB_UDF_ICBTAG_TYPE_SYMLINK 0x0C
  68. #define GRUB_UDF_ICBTAG_TYPE_STREAMDIR 0x0D
  69. #define GRUB_UDF_ICBTAG_FLAG_AD_MASK 0x0007
  70. #define GRUB_UDF_ICBTAG_FLAG_AD_SHORT 0x0000
  71. #define GRUB_UDF_ICBTAG_FLAG_AD_LONG 0x0001
  72. #define GRUB_UDF_ICBTAG_FLAG_AD_EXT 0x0002
  73. #define GRUB_UDF_ICBTAG_FLAG_AD_IN_ICB 0x0003
  74. #define GRUB_UDF_EXT_NORMAL 0x00000000
  75. #define GRUB_UDF_EXT_NREC_ALLOC 0x40000000
  76. #define GRUB_UDF_EXT_NREC_NALLOC 0x80000000
  77. #define GRUB_UDF_EXT_MASK 0xC0000000
  78. #define GRUB_UDF_FID_CHAR_HIDDEN 0x01
  79. #define GRUB_UDF_FID_CHAR_DIRECTORY 0x02
  80. #define GRUB_UDF_FID_CHAR_DELETED 0x04
  81. #define GRUB_UDF_FID_CHAR_PARENT 0x08
  82. #define GRUB_UDF_FID_CHAR_METADATA 0x10
  83. #define GRUB_UDF_STD_IDENT_BEA01 "BEA01"
  84. #define GRUB_UDF_STD_IDENT_BOOT2 "BOOT2"
  85. #define GRUB_UDF_STD_IDENT_CD001 "CD001"
  86. #define GRUB_UDF_STD_IDENT_CDW02 "CDW02"
  87. #define GRUB_UDF_STD_IDENT_NSR02 "NSR02"
  88. #define GRUB_UDF_STD_IDENT_NSR03 "NSR03"
  89. #define GRUB_UDF_STD_IDENT_TEA01 "TEA01"
  90. #define GRUB_UDF_CHARSPEC_TYPE_CS0 0x00
  91. #define GRUB_UDF_CHARSPEC_TYPE_CS1 0x01
  92. #define GRUB_UDF_CHARSPEC_TYPE_CS2 0x02
  93. #define GRUB_UDF_CHARSPEC_TYPE_CS3 0x03
  94. #define GRUB_UDF_CHARSPEC_TYPE_CS4 0x04
  95. #define GRUB_UDF_CHARSPEC_TYPE_CS5 0x05
  96. #define GRUB_UDF_CHARSPEC_TYPE_CS6 0x06
  97. #define GRUB_UDF_CHARSPEC_TYPE_CS7 0x07
  98. #define GRUB_UDF_CHARSPEC_TYPE_CS8 0x08
  99. #define GRUB_UDF_PARTMAP_TYPE_1 1
  100. #define GRUB_UDF_PARTMAP_TYPE_2 2
  101. struct grub_udf_lb_addr
  102. {
  103. grub_uint32_t block_num;
  104. grub_uint16_t part_ref;
  105. } __attribute__ ((packed));
  106. struct grub_udf_short_ad
  107. {
  108. grub_uint32_t length;
  109. grub_uint32_t position;
  110. } __attribute__ ((packed));
  111. struct grub_udf_long_ad
  112. {
  113. grub_uint32_t length;
  114. struct grub_udf_lb_addr block;
  115. grub_uint8_t imp_use[6];
  116. } __attribute__ ((packed));
  117. struct grub_udf_extent_ad
  118. {
  119. grub_uint32_t length;
  120. grub_uint32_t start;
  121. } __attribute__ ((packed));
  122. struct grub_udf_charspec
  123. {
  124. grub_uint8_t charset_type;
  125. grub_uint8_t charset_info[63];
  126. } __attribute__ ((packed));
  127. struct grub_udf_timestamp
  128. {
  129. grub_uint16_t type_and_timezone;
  130. grub_uint16_t year;
  131. grub_uint8_t month;
  132. grub_uint8_t day;
  133. grub_uint8_t hour;
  134. grub_uint8_t minute;
  135. grub_uint8_t second;
  136. grub_uint8_t centi_seconds;
  137. grub_uint8_t hundreds_of_micro_seconds;
  138. grub_uint8_t micro_seconds;
  139. } __attribute__ ((packed));
  140. struct grub_udf_regid
  141. {
  142. grub_uint8_t flags;
  143. grub_uint8_t ident[23];
  144. grub_uint8_t ident_suffix[8];
  145. } __attribute__ ((packed));
  146. struct grub_udf_tag
  147. {
  148. grub_uint16_t tag_ident;
  149. grub_uint16_t desc_version;
  150. grub_uint8_t tag_checksum;
  151. grub_uint8_t reserved;
  152. grub_uint16_t tag_serial_number;
  153. grub_uint16_t desc_crc;
  154. grub_uint16_t desc_crc_length;
  155. grub_uint32_t tag_location;
  156. } __attribute__ ((packed));
  157. struct grub_udf_fileset
  158. {
  159. struct grub_udf_tag tag;
  160. struct grub_udf_timestamp datetime;
  161. grub_uint16_t interchange_level;
  162. grub_uint16_t max_interchange_level;
  163. grub_uint32_t charset_list;
  164. grub_uint32_t max_charset_list;
  165. grub_uint32_t fileset_num;
  166. grub_uint32_t fileset_desc_num;
  167. struct grub_udf_charspec vol_charset;
  168. grub_uint8_t vol_ident[128];
  169. struct grub_udf_charspec fileset_charset;
  170. grub_uint8_t fileset_ident[32];
  171. grub_uint8_t copyright_file_ident[32];
  172. grub_uint8_t abstract_file_ident[32];
  173. struct grub_udf_long_ad root_icb;
  174. struct grub_udf_regid domain_ident;
  175. struct grub_udf_long_ad next_ext;
  176. struct grub_udf_long_ad streamdir_icb;
  177. } __attribute__ ((packed));
  178. struct grub_udf_icbtag
  179. {
  180. grub_uint32_t prior_recorded_num_direct_entries;
  181. grub_uint16_t strategy_type;
  182. grub_uint16_t strategy_parameter;
  183. grub_uint16_t num_entries;
  184. grub_uint8_t reserved;
  185. grub_uint8_t file_type;
  186. struct grub_udf_lb_addr parent_idb;
  187. grub_uint16_t flags;
  188. } __attribute__ ((packed));
  189. struct grub_udf_file_ident
  190. {
  191. struct grub_udf_tag tag;
  192. grub_uint16_t version_num;
  193. grub_uint8_t characteristics;
  194. grub_uint8_t file_ident_length;
  195. struct grub_udf_long_ad icb;
  196. grub_uint16_t imp_use_length;
  197. } __attribute__ ((packed));
  198. struct grub_udf_file_entry
  199. {
  200. struct grub_udf_tag tag;
  201. struct grub_udf_icbtag icbtag;
  202. grub_uint32_t uid;
  203. grub_uint32_t gid;
  204. grub_uint32_t permissions;
  205. grub_uint16_t link_count;
  206. grub_uint8_t record_format;
  207. grub_uint8_t record_display_attr;
  208. grub_uint32_t record_length;
  209. grub_uint64_t file_size;
  210. grub_uint64_t blocks_recorded;
  211. struct grub_udf_timestamp access_time;
  212. struct grub_udf_timestamp modification_time;
  213. struct grub_udf_timestamp attr_time;
  214. grub_uint32_t checkpoint;
  215. struct grub_udf_long_ad extended_attr_idb;
  216. struct grub_udf_regid imp_ident;
  217. grub_uint64_t unique_id;
  218. grub_uint32_t ext_attr_length;
  219. grub_uint32_t alloc_descs_length;
  220. grub_uint8_t ext_attr[1872];
  221. } __attribute__ ((packed));
  222. struct grub_udf_extended_file_entry
  223. {
  224. struct grub_udf_tag tag;
  225. struct grub_udf_icbtag icbtag;
  226. grub_uint32_t uid;
  227. grub_uint32_t gid;
  228. grub_uint32_t permissions;
  229. grub_uint16_t link_count;
  230. grub_uint8_t record_format;
  231. grub_uint8_t record_display_attr;
  232. grub_uint32_t record_length;
  233. grub_uint64_t file_size;
  234. grub_uint64_t object_size;
  235. grub_uint64_t blocks_recorded;
  236. struct grub_udf_timestamp access_time;
  237. struct grub_udf_timestamp modification_time;
  238. struct grub_udf_timestamp create_time;
  239. struct grub_udf_timestamp attr_time;
  240. grub_uint32_t checkpoint;
  241. grub_uint32_t reserved;
  242. struct grub_udf_long_ad extended_attr_icb;
  243. struct grub_udf_long_ad streamdir_icb;
  244. struct grub_udf_regid imp_ident;
  245. grub_uint64_t unique_id;
  246. grub_uint32_t ext_attr_length;
  247. grub_uint32_t alloc_descs_length;
  248. grub_uint8_t ext_attr[1832];
  249. } __attribute__ ((packed));
  250. struct grub_udf_vrs
  251. {
  252. grub_uint8_t type;
  253. grub_uint8_t magic[5];
  254. grub_uint8_t version;
  255. } __attribute__ ((packed));
  256. struct grub_udf_avdp
  257. {
  258. struct grub_udf_tag tag;
  259. struct grub_udf_extent_ad vds;
  260. } __attribute__ ((packed));
  261. struct grub_udf_pd
  262. {
  263. struct grub_udf_tag tag;
  264. grub_uint32_t seq_num;
  265. grub_uint16_t flags;
  266. grub_uint16_t part_num;
  267. struct grub_udf_regid contents;
  268. grub_uint8_t contents_use[128];
  269. grub_uint32_t access_type;
  270. grub_uint32_t start;
  271. grub_uint32_t length;
  272. } __attribute__ ((packed));
  273. struct grub_udf_partmap
  274. {
  275. grub_uint8_t type;
  276. grub_uint8_t length;
  277. union
  278. {
  279. struct
  280. {
  281. grub_uint16_t seq_num;
  282. grub_uint16_t part_num;
  283. } type1;
  284. struct
  285. {
  286. grub_uint8_t ident[62];
  287. } type2;
  288. };
  289. };
  290. struct grub_udf_lvd
  291. {
  292. struct grub_udf_tag tag;
  293. grub_uint32_t seq_num;
  294. struct grub_udf_charspec charset;
  295. grub_uint8_t ident[128];
  296. grub_uint32_t bsize;
  297. struct grub_udf_regid domain_ident;
  298. struct grub_udf_long_ad root_fileset;
  299. grub_uint32_t map_table_length;
  300. grub_uint32_t num_part_maps;
  301. struct grub_udf_regid imp_ident;
  302. grub_uint8_t imp_use[128];
  303. struct grub_udf_extent_ad integrity_seq_ext;
  304. grub_uint8_t part_maps[1608];
  305. } __attribute__ ((packed));
  306. struct grub_udf_data
  307. {
  308. grub_disk_t disk;
  309. struct grub_udf_lvd lvd;
  310. struct grub_udf_pd pds[GRUB_UDF_MAX_PDS];
  311. struct grub_udf_partmap *pms[GRUB_UDF_MAX_PMS];
  312. struct grub_udf_long_ad root_icb;
  313. int npd, npm;
  314. };
  315. struct grub_fshelp_node
  316. {
  317. struct grub_udf_data *data;
  318. union
  319. {
  320. struct grub_udf_file_entry fe;
  321. struct grub_udf_extended_file_entry efe;
  322. };
  323. int part_ref;
  324. };
  325. static grub_dl_t my_mod;
  326. static grub_uint32_t
  327. grub_udf_get_block (struct grub_udf_data *data,
  328. grub_uint16_t part_ref, grub_uint32_t block)
  329. {
  330. part_ref = U16 (part_ref);
  331. if (part_ref >= data->npm)
  332. {
  333. grub_error (GRUB_ERR_BAD_FS, "invalid part ref");
  334. return 0;
  335. }
  336. return (U32 (data->pds[data->pms[part_ref]->type1.part_num].start)
  337. + U32 (block));
  338. }
  339. static grub_err_t
  340. grub_udf_read_icb (struct grub_udf_data *data,
  341. struct grub_udf_long_ad *icb,
  342. struct grub_fshelp_node *node)
  343. {
  344. grub_uint32_t block;
  345. block = grub_udf_get_block (data,
  346. icb->block.part_ref,
  347. icb->block.block_num);
  348. if (grub_errno)
  349. return grub_errno;
  350. if (grub_disk_read (data->disk, block << GRUB_UDF_LOG2_BLKSZ, 0,
  351. sizeof (struct grub_udf_file_entry),
  352. &node->fe))
  353. return grub_errno;
  354. if ((U16 (node->fe.tag.tag_ident) != GRUB_UDF_TAG_IDENT_FE) &&
  355. (U16 (node->fe.tag.tag_ident) != GRUB_UDF_TAG_IDENT_EFE))
  356. return grub_error (GRUB_ERR_BAD_FS, "invalid fe/efe descriptor");
  357. node->part_ref = icb->block.part_ref;
  358. node->data = data;
  359. return 0;
  360. }
  361. static grub_disk_addr_t
  362. grub_udf_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
  363. {
  364. char *ptr;
  365. int len;
  366. grub_disk_addr_t filebytes;
  367. if (U16 (node->fe.tag.tag_ident) == GRUB_UDF_TAG_IDENT_FE)
  368. {
  369. ptr = (char *) &node->fe.ext_attr[0] + U32 (node->fe.ext_attr_length);
  370. len = U32 (node->fe.alloc_descs_length);
  371. }
  372. else
  373. {
  374. ptr = (char *) &node->efe.ext_attr[0] + U32 (node->efe.ext_attr_length);
  375. len = U32 (node->efe.alloc_descs_length);
  376. }
  377. if ((U16 (node->fe.icbtag.flags) & GRUB_UDF_ICBTAG_FLAG_AD_MASK)
  378. == GRUB_UDF_ICBTAG_FLAG_AD_SHORT)
  379. {
  380. struct grub_udf_short_ad *ad = (struct grub_udf_short_ad *) ptr;
  381. len /= sizeof (struct grub_udf_short_ad);
  382. filebytes = fileblock * GRUB_UDF_BLKSZ;
  383. while (len > 0)
  384. {
  385. if (filebytes < U32 (ad->length))
  386. return ((U32 (ad->position) & GRUB_UDF_EXT_MASK) ? 0 :
  387. (grub_udf_get_block (node->data,
  388. node->part_ref,
  389. ad->position)
  390. + (filebytes / GRUB_UDF_BLKSZ)));
  391. filebytes -= U32 (ad->length);
  392. ad++;
  393. len--;
  394. }
  395. }
  396. else
  397. {
  398. struct grub_udf_long_ad *ad = (struct grub_udf_long_ad *) ptr;
  399. len /= sizeof (struct grub_udf_long_ad);
  400. filebytes = fileblock * GRUB_UDF_BLKSZ;
  401. while (len > 0)
  402. {
  403. if (filebytes < U32 (ad->length))
  404. return ((U32 (ad->block.block_num) & GRUB_UDF_EXT_MASK) ? 0 :
  405. (grub_udf_get_block (node->data,
  406. ad->block.part_ref,
  407. ad->block.block_num)
  408. + (filebytes / GRUB_UDF_BLKSZ)));
  409. filebytes -= U32 (ad->length);
  410. ad++;
  411. len--;
  412. }
  413. }
  414. return 0;
  415. }
  416. static grub_ssize_t
  417. grub_udf_read_file (grub_fshelp_node_t node,
  418. void (*read_hook) (grub_disk_addr_t sector,
  419. unsigned offset, unsigned length,
  420. void *closure),
  421. void *closure, int flags,
  422. int pos, grub_size_t len, char *buf)
  423. {
  424. switch (U16 (node->fe.icbtag.flags) & GRUB_UDF_ICBTAG_FLAG_AD_MASK)
  425. {
  426. case GRUB_UDF_ICBTAG_FLAG_AD_IN_ICB:
  427. {
  428. char *ptr;
  429. ptr = ((U16 (node->fe.tag.tag_ident) == GRUB_UDF_TAG_IDENT_FE) ?
  430. ((char *) &node->fe.ext_attr[0]
  431. + U32 (node->fe.ext_attr_length)) :
  432. ((char *) &node->efe.ext_attr[0]
  433. + U32 (node->efe.ext_attr_length)));
  434. grub_memcpy (buf, ptr + pos, len);
  435. return len;
  436. }
  437. case GRUB_UDF_ICBTAG_FLAG_AD_EXT:
  438. grub_error (GRUB_ERR_BAD_FS, "invalid extent type");
  439. return 0;
  440. }
  441. return grub_fshelp_read_file (node->data->disk, node, read_hook, closure,
  442. flags, pos, len, buf, grub_udf_read_block,
  443. U64 (node->fe.file_size),
  444. GRUB_UDF_LOG2_BLKSZ);
  445. }
  446. static int sblocklist[] = { 256, 512, 0 };
  447. static struct grub_udf_data *
  448. grub_udf_mount (grub_disk_t disk)
  449. {
  450. struct grub_udf_data *data = 0;
  451. struct grub_udf_fileset root_fs;
  452. int *sblklist = sblocklist;
  453. grub_uint32_t block;
  454. int i;
  455. data = grub_malloc (sizeof (struct grub_udf_data));
  456. if (!data)
  457. return 0;
  458. data->disk = disk;
  459. /* Search for Volume Recognition Sequence (VRS). */
  460. for (block = 16;; block++)
  461. {
  462. struct grub_udf_vrs vrs;
  463. if (grub_disk_read (disk, block << GRUB_UDF_LOG2_BLKSZ, 0,
  464. sizeof (struct grub_udf_vrs), &vrs))
  465. {
  466. grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem");
  467. goto fail;
  468. }
  469. if ((!grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_NSR03, 5)) ||
  470. (!grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_NSR02, 5)))
  471. break;
  472. if ((grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_BEA01, 5)) &&
  473. (grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_BOOT2, 5)) &&
  474. (grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_CD001, 5)) &&
  475. (grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_CDW02, 5)) &&
  476. (grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_TEA01, 5)))
  477. {
  478. grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem");
  479. goto fail;
  480. }
  481. }
  482. /* Search for Anchor Volume Descriptor Pointer (AVDP). */
  483. while (1)
  484. {
  485. struct grub_udf_avdp avdp;
  486. if (grub_disk_read (disk, *sblklist << GRUB_UDF_LOG2_BLKSZ, 0,
  487. sizeof (struct grub_udf_avdp), &avdp))
  488. {
  489. grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem");
  490. goto fail;
  491. }
  492. if (U16 (avdp.tag.tag_ident) == GRUB_UDF_TAG_IDENT_AVDP)
  493. {
  494. block = U32 (avdp.vds.start);
  495. break;
  496. }
  497. sblklist++;
  498. if (*sblklist == 0)
  499. {
  500. grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem");
  501. goto fail;
  502. }
  503. }
  504. data->npd = data->npm = 0;
  505. /* Locate Partition Descriptor (PD) and Logical Volume Descriptor (LVD). */
  506. while (1)
  507. {
  508. struct grub_udf_tag tag;
  509. if (grub_disk_read (disk, block << GRUB_UDF_LOG2_BLKSZ, 0,
  510. sizeof (struct grub_udf_tag), &tag))
  511. {
  512. grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem");
  513. goto fail;
  514. }
  515. tag.tag_ident = U16 (tag.tag_ident);
  516. if (tag.tag_ident == GRUB_UDF_TAG_IDENT_PD)
  517. {
  518. if (data->npd >= GRUB_UDF_MAX_PDS)
  519. {
  520. grub_error (GRUB_ERR_BAD_FS, "too many PDs");
  521. goto fail;
  522. }
  523. if (grub_disk_read (disk, block << GRUB_UDF_LOG2_BLKSZ, 0,
  524. sizeof (struct grub_udf_pd),
  525. &data->pds[data->npd]))
  526. {
  527. grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem");
  528. goto fail;
  529. }
  530. data->npd++;
  531. }
  532. else if (tag.tag_ident == GRUB_UDF_TAG_IDENT_LVD)
  533. {
  534. int k;
  535. struct grub_udf_partmap *ppm;
  536. if (grub_disk_read (disk, block << GRUB_UDF_LOG2_BLKSZ, 0,
  537. sizeof (struct grub_udf_lvd),
  538. &data->lvd))
  539. {
  540. grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem");
  541. goto fail;
  542. }
  543. if (data->npm + U32 (data->lvd.num_part_maps) > GRUB_UDF_MAX_PMS)
  544. {
  545. grub_error (GRUB_ERR_BAD_FS, "too many partition maps");
  546. goto fail;
  547. }
  548. ppm = (struct grub_udf_partmap *) &data->lvd.part_maps;
  549. for (k = U32 (data->lvd.num_part_maps); k > 0; k--)
  550. {
  551. if (ppm->type != GRUB_UDF_PARTMAP_TYPE_1)
  552. {
  553. grub_error (GRUB_ERR_BAD_FS, "partmap type not supported");
  554. goto fail;
  555. }
  556. data->pms[data->npm++] = ppm;
  557. ppm = (struct grub_udf_partmap *) ((char *) ppm +
  558. U32 (ppm->length));
  559. }
  560. }
  561. else if (tag.tag_ident > GRUB_UDF_TAG_IDENT_TD)
  562. {
  563. grub_error (GRUB_ERR_BAD_FS, "invalid tag ident");
  564. goto fail;
  565. }
  566. else if (tag.tag_ident == GRUB_UDF_TAG_IDENT_TD)
  567. break;
  568. block++;
  569. }
  570. for (i = 0; i < data->npm; i++)
  571. {
  572. int j;
  573. for (j = 0; j < data->npd; j++)
  574. if (data->pms[i]->type1.part_num == data->pds[j].part_num)
  575. {
  576. data->pms[i]->type1.part_num = j;
  577. break;
  578. }
  579. if (j == data->npd)
  580. {
  581. grub_error (GRUB_ERR_BAD_FS, "can\'t find PD");
  582. goto fail;
  583. }
  584. }
  585. block = grub_udf_get_block (data,
  586. data->lvd.root_fileset.block.part_ref,
  587. data->lvd.root_fileset.block.block_num);
  588. if (grub_errno)
  589. goto fail;
  590. if (grub_disk_read (disk, block << GRUB_UDF_LOG2_BLKSZ, 0,
  591. sizeof (struct grub_udf_fileset), &root_fs))
  592. {
  593. grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem");
  594. goto fail;
  595. }
  596. if (U16 (root_fs.tag.tag_ident) != GRUB_UDF_TAG_IDENT_FSD)
  597. {
  598. grub_error (GRUB_ERR_BAD_FS, "invalid fileset descriptor");
  599. goto fail;
  600. }
  601. data->root_icb = root_fs.root_icb;
  602. return data;
  603. fail:
  604. grub_free (data);
  605. return 0;
  606. }
  607. static int
  608. grub_udf_iterate_dir (grub_fshelp_node_t dir,
  609. int (*hook) (const char *filename,
  610. enum grub_fshelp_filetype filetype,
  611. grub_fshelp_node_t node,
  612. void *closure),
  613. void *closure)
  614. {
  615. grub_fshelp_node_t child;
  616. struct grub_udf_file_ident dirent;
  617. grub_uint32_t offset = 0;
  618. child = grub_malloc (sizeof (struct grub_fshelp_node));
  619. if (!child)
  620. return 0;
  621. /* The current directory is not stored. */
  622. grub_memcpy ((char *) child, (char *) dir,
  623. sizeof (struct grub_fshelp_node));
  624. if (hook (".", GRUB_FSHELP_DIR, child, closure))
  625. return 1;
  626. while (offset < U64 (dir->fe.file_size))
  627. {
  628. if (grub_udf_read_file (dir, 0, 0, 0, offset, sizeof (dirent),
  629. (char *) &dirent) != sizeof (dirent))
  630. return 0;
  631. if (U16 (dirent.tag.tag_ident) != GRUB_UDF_TAG_IDENT_FID)
  632. {
  633. grub_error (GRUB_ERR_BAD_FS, "invalid fid tag");
  634. return 0;
  635. }
  636. child = grub_malloc (sizeof (struct grub_fshelp_node));
  637. if (!child)
  638. return 0;
  639. if (grub_udf_read_icb (dir->data, &dirent.icb, child))
  640. return 0;
  641. offset += sizeof (dirent) + U16 (dirent.imp_use_length);
  642. if (dirent.characteristics & GRUB_UDF_FID_CHAR_PARENT)
  643. {
  644. /* This is the parent directory. */
  645. if (hook ("..", GRUB_FSHELP_DIR, child, closure))
  646. return 1;
  647. }
  648. else
  649. {
  650. enum grub_fshelp_filetype type;
  651. grub_uint8_t raw[dirent.file_ident_length];
  652. grub_uint16_t utf16[dirent.file_ident_length - 1];
  653. grub_uint8_t filename[dirent.file_ident_length * 2];
  654. grub_size_t utf16len = 0;
  655. type = ((dirent.characteristics & GRUB_UDF_FID_CHAR_DIRECTORY) ?
  656. (GRUB_FSHELP_DIR) : (GRUB_FSHELP_REG));
  657. if ((grub_udf_read_file (dir, 0, 0, 0, offset,
  658. dirent.file_ident_length,
  659. (char *) raw))
  660. != dirent.file_ident_length)
  661. return 0;
  662. if (raw[0] == 8)
  663. {
  664. unsigned i;
  665. utf16len = dirent.file_ident_length - 1;
  666. for (i = 0; i < utf16len; i++)
  667. utf16[i] = raw[i + 1];
  668. }
  669. if (raw[0] == 16)
  670. {
  671. unsigned i;
  672. utf16len = (dirent.file_ident_length - 1) / 2;
  673. for (i = 0; i < utf16len; i++)
  674. utf16[i] = (raw[2 * i + 1] << 8) | raw[2*i + 2];
  675. }
  676. if (raw[0] == 8 || raw[0] == 16)
  677. {
  678. *grub_utf16_to_utf8 (filename, utf16, utf16len) = '\0';
  679. if (hook ((char *) filename, type, child, closure))
  680. return 1;
  681. }
  682. }
  683. /* Align to dword boundary. */
  684. offset = (offset + dirent.file_ident_length + 3) & (~3);
  685. }
  686. return 0;
  687. }
  688. struct grub_udf_dir_closure
  689. {
  690. int (*hook) (const char *filename,
  691. const struct grub_dirhook_info *info,
  692. void *closure);
  693. void *closure;
  694. };
  695. static int
  696. iterate (const char *filename,
  697. enum grub_fshelp_filetype filetype,
  698. grub_fshelp_node_t node,
  699. void *closure)
  700. {
  701. struct grub_udf_dir_closure *c = closure;
  702. struct grub_dirhook_info info;
  703. grub_memset (&info, 0, sizeof (info));
  704. info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
  705. grub_free (node);
  706. return c->hook (filename, &info, c->closure);
  707. }
  708. static grub_err_t
  709. grub_udf_dir (grub_device_t device, const char *path,
  710. int (*hook) (const char *filename,
  711. const struct grub_dirhook_info *info,
  712. void *closure),
  713. void *closure)
  714. {
  715. struct grub_udf_data *data = 0;
  716. struct grub_fshelp_node rootnode;
  717. struct grub_fshelp_node *foundnode;
  718. struct grub_udf_dir_closure c;
  719. grub_dl_ref (my_mod);
  720. data = grub_udf_mount (device->disk);
  721. if (!data)
  722. goto fail;
  723. if (grub_udf_read_icb (data, &data->root_icb, &rootnode))
  724. goto fail;
  725. if (grub_fshelp_find_file (path, &rootnode,
  726. &foundnode,
  727. grub_udf_iterate_dir, 0, 0, GRUB_FSHELP_DIR))
  728. goto fail;
  729. c.hook = hook;
  730. c.closure = closure;
  731. grub_udf_iterate_dir (foundnode, iterate, &c);
  732. if (foundnode != &rootnode)
  733. grub_free (foundnode);
  734. fail:
  735. grub_free (data);
  736. grub_dl_unref (my_mod);
  737. return grub_errno;
  738. }
  739. static grub_err_t
  740. grub_udf_open (struct grub_file *file, const char *name)
  741. {
  742. struct grub_udf_data *data;
  743. struct grub_fshelp_node rootnode;
  744. struct grub_fshelp_node *foundnode;
  745. grub_dl_ref (my_mod);
  746. data = grub_udf_mount (file->device->disk);
  747. if (!data)
  748. goto fail;
  749. if (grub_udf_read_icb (data, &data->root_icb, &rootnode))
  750. goto fail;
  751. if (grub_fshelp_find_file (name, &rootnode,
  752. &foundnode,
  753. grub_udf_iterate_dir, 0, 0, GRUB_FSHELP_REG))
  754. goto fail;
  755. file->data = foundnode;
  756. file->offset = 0;
  757. file->size = U64 (foundnode->fe.file_size);
  758. return 0;
  759. fail:
  760. grub_dl_unref (my_mod);
  761. grub_free (data);
  762. return grub_errno;
  763. }
  764. static grub_ssize_t
  765. grub_udf_read (grub_file_t file, char *buf, grub_size_t len)
  766. {
  767. struct grub_fshelp_node *node = (struct grub_fshelp_node *) file->data;
  768. return grub_udf_read_file (node, file->read_hook, file->closure,
  769. file->flags, file->offset, len, buf);
  770. }
  771. static grub_err_t
  772. grub_udf_close (grub_file_t file)
  773. {
  774. if (file->data)
  775. {
  776. struct grub_fshelp_node *node = (struct grub_fshelp_node *) file->data;
  777. grub_free (node->data);
  778. grub_free (node);
  779. }
  780. grub_dl_unref (my_mod);
  781. return GRUB_ERR_NONE;
  782. }
  783. static grub_err_t
  784. grub_udf_label (grub_device_t device, char **label)
  785. {
  786. struct grub_udf_data *data;
  787. data = grub_udf_mount (device->disk);
  788. if (data)
  789. {
  790. *label = grub_strdup ((char *) &data->lvd.ident[1]);
  791. grub_free (data);
  792. }
  793. else
  794. *label = 0;
  795. return grub_errno;
  796. }
  797. static struct grub_fs grub_udf_fs = {
  798. .name = "udf",
  799. .dir = grub_udf_dir,
  800. .open = grub_udf_open,
  801. .read = grub_udf_read,
  802. .close = grub_udf_close,
  803. .label = grub_udf_label,
  804. .next = 0
  805. };
  806. GRUB_MOD_INIT (udf)
  807. {
  808. grub_fs_register (&grub_udf_fs);
  809. my_mod = mod;
  810. }
  811. GRUB_MOD_FINI (udf)
  812. {
  813. grub_fs_unregister (&grub_udf_fs);
  814. }