udf.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  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. #include <grub/datetime.h>
  29. #include <grub/udf.h>
  30. GRUB_MOD_LICENSE ("GPLv3+");
  31. #define GRUB_UDF_MAX_PDS 2
  32. #define GRUB_UDF_MAX_PMS 6
  33. #define U16 grub_le_to_cpu16
  34. #define U32 grub_le_to_cpu32
  35. #define U64 grub_le_to_cpu64
  36. #define GRUB_UDF_TAG_IDENT_PVD 0x0001
  37. #define GRUB_UDF_TAG_IDENT_AVDP 0x0002
  38. #define GRUB_UDF_TAG_IDENT_VDP 0x0003
  39. #define GRUB_UDF_TAG_IDENT_IUVD 0x0004
  40. #define GRUB_UDF_TAG_IDENT_PD 0x0005
  41. #define GRUB_UDF_TAG_IDENT_LVD 0x0006
  42. #define GRUB_UDF_TAG_IDENT_USD 0x0007
  43. #define GRUB_UDF_TAG_IDENT_TD 0x0008
  44. #define GRUB_UDF_TAG_IDENT_LVID 0x0009
  45. #define GRUB_UDF_TAG_IDENT_FSD 0x0100
  46. #define GRUB_UDF_TAG_IDENT_FID 0x0101
  47. #define GRUB_UDF_TAG_IDENT_AED 0x0102
  48. #define GRUB_UDF_TAG_IDENT_IE 0x0103
  49. #define GRUB_UDF_TAG_IDENT_TE 0x0104
  50. #define GRUB_UDF_TAG_IDENT_FE 0x0105
  51. #define GRUB_UDF_TAG_IDENT_EAHD 0x0106
  52. #define GRUB_UDF_TAG_IDENT_USE 0x0107
  53. #define GRUB_UDF_TAG_IDENT_SBD 0x0108
  54. #define GRUB_UDF_TAG_IDENT_PIE 0x0109
  55. #define GRUB_UDF_TAG_IDENT_EFE 0x010A
  56. #define GRUB_UDF_ICBTAG_TYPE_UNDEF 0x00
  57. #define GRUB_UDF_ICBTAG_TYPE_USE 0x01
  58. #define GRUB_UDF_ICBTAG_TYPE_PIE 0x02
  59. #define GRUB_UDF_ICBTAG_TYPE_IE 0x03
  60. #define GRUB_UDF_ICBTAG_TYPE_DIRECTORY 0x04
  61. #define GRUB_UDF_ICBTAG_TYPE_REGULAR 0x05
  62. #define GRUB_UDF_ICBTAG_TYPE_BLOCK 0x06
  63. #define GRUB_UDF_ICBTAG_TYPE_CHAR 0x07
  64. #define GRUB_UDF_ICBTAG_TYPE_EA 0x08
  65. #define GRUB_UDF_ICBTAG_TYPE_FIFO 0x09
  66. #define GRUB_UDF_ICBTAG_TYPE_SOCKET 0x0A
  67. #define GRUB_UDF_ICBTAG_TYPE_TE 0x0B
  68. #define GRUB_UDF_ICBTAG_TYPE_SYMLINK 0x0C
  69. #define GRUB_UDF_ICBTAG_TYPE_STREAMDIR 0x0D
  70. #define GRUB_UDF_ICBTAG_FLAG_AD_MASK 0x0007
  71. #define GRUB_UDF_ICBTAG_FLAG_AD_SHORT 0x0000
  72. #define GRUB_UDF_ICBTAG_FLAG_AD_LONG 0x0001
  73. #define GRUB_UDF_ICBTAG_FLAG_AD_EXT 0x0002
  74. #define GRUB_UDF_ICBTAG_FLAG_AD_IN_ICB 0x0003
  75. #define GRUB_UDF_EXT_NORMAL 0x00000000
  76. #define GRUB_UDF_EXT_NREC_ALLOC 0x40000000
  77. #define GRUB_UDF_EXT_NREC_NALLOC 0x80000000
  78. #define GRUB_UDF_EXT_MASK 0xC0000000
  79. #define GRUB_UDF_FID_CHAR_HIDDEN 0x01
  80. #define GRUB_UDF_FID_CHAR_DIRECTORY 0x02
  81. #define GRUB_UDF_FID_CHAR_DELETED 0x04
  82. #define GRUB_UDF_FID_CHAR_PARENT 0x08
  83. #define GRUB_UDF_FID_CHAR_METADATA 0x10
  84. #define GRUB_UDF_STD_IDENT_BEA01 "BEA01"
  85. #define GRUB_UDF_STD_IDENT_BOOT2 "BOOT2"
  86. #define GRUB_UDF_STD_IDENT_CD001 "CD001"
  87. #define GRUB_UDF_STD_IDENT_CDW02 "CDW02"
  88. #define GRUB_UDF_STD_IDENT_NSR02 "NSR02"
  89. #define GRUB_UDF_STD_IDENT_NSR03 "NSR03"
  90. #define GRUB_UDF_STD_IDENT_TEA01 "TEA01"
  91. #define GRUB_UDF_CHARSPEC_TYPE_CS0 0x00
  92. #define GRUB_UDF_CHARSPEC_TYPE_CS1 0x01
  93. #define GRUB_UDF_CHARSPEC_TYPE_CS2 0x02
  94. #define GRUB_UDF_CHARSPEC_TYPE_CS3 0x03
  95. #define GRUB_UDF_CHARSPEC_TYPE_CS4 0x04
  96. #define GRUB_UDF_CHARSPEC_TYPE_CS5 0x05
  97. #define GRUB_UDF_CHARSPEC_TYPE_CS6 0x06
  98. #define GRUB_UDF_CHARSPEC_TYPE_CS7 0x07
  99. #define GRUB_UDF_CHARSPEC_TYPE_CS8 0x08
  100. #define GRUB_UDF_PARTMAP_TYPE_1 1
  101. #define GRUB_UDF_PARTMAP_TYPE_2 2
  102. struct grub_udf_lb_addr
  103. {
  104. grub_uint32_t block_num;
  105. grub_uint16_t part_ref;
  106. } GRUB_PACKED;
  107. struct grub_udf_short_ad
  108. {
  109. grub_uint32_t length;
  110. grub_uint32_t position;
  111. } GRUB_PACKED;
  112. struct grub_udf_long_ad
  113. {
  114. grub_uint32_t length;
  115. struct grub_udf_lb_addr block;
  116. grub_uint8_t imp_use[6];
  117. } GRUB_PACKED;
  118. struct grub_udf_extent_ad
  119. {
  120. grub_uint32_t length;
  121. grub_uint32_t start;
  122. } GRUB_PACKED;
  123. struct grub_udf_charspec
  124. {
  125. grub_uint8_t charset_type;
  126. grub_uint8_t charset_info[63];
  127. } GRUB_PACKED;
  128. struct grub_udf_timestamp
  129. {
  130. grub_uint16_t type_and_timezone;
  131. grub_uint16_t year;
  132. grub_uint8_t month;
  133. grub_uint8_t day;
  134. grub_uint8_t hour;
  135. grub_uint8_t minute;
  136. grub_uint8_t second;
  137. grub_uint8_t centi_seconds;
  138. grub_uint8_t hundreds_of_micro_seconds;
  139. grub_uint8_t micro_seconds;
  140. } GRUB_PACKED;
  141. struct grub_udf_regid
  142. {
  143. grub_uint8_t flags;
  144. grub_uint8_t ident[23];
  145. grub_uint8_t ident_suffix[8];
  146. } GRUB_PACKED;
  147. struct grub_udf_tag
  148. {
  149. grub_uint16_t tag_ident;
  150. grub_uint16_t desc_version;
  151. grub_uint8_t tag_checksum;
  152. grub_uint8_t reserved;
  153. grub_uint16_t tag_serial_number;
  154. grub_uint16_t desc_crc;
  155. grub_uint16_t desc_crc_length;
  156. grub_uint32_t tag_location;
  157. } GRUB_PACKED;
  158. struct grub_udf_fileset
  159. {
  160. struct grub_udf_tag tag;
  161. struct grub_udf_timestamp datetime;
  162. grub_uint16_t interchange_level;
  163. grub_uint16_t max_interchange_level;
  164. grub_uint32_t charset_list;
  165. grub_uint32_t max_charset_list;
  166. grub_uint32_t fileset_num;
  167. grub_uint32_t fileset_desc_num;
  168. struct grub_udf_charspec vol_charset;
  169. grub_uint8_t vol_ident[128];
  170. struct grub_udf_charspec fileset_charset;
  171. grub_uint8_t fileset_ident[32];
  172. grub_uint8_t copyright_file_ident[32];
  173. grub_uint8_t abstract_file_ident[32];
  174. struct grub_udf_long_ad root_icb;
  175. struct grub_udf_regid domain_ident;
  176. struct grub_udf_long_ad next_ext;
  177. struct grub_udf_long_ad streamdir_icb;
  178. } GRUB_PACKED;
  179. struct grub_udf_icbtag
  180. {
  181. grub_uint32_t prior_recorded_num_direct_entries;
  182. grub_uint16_t strategy_type;
  183. grub_uint16_t strategy_parameter;
  184. grub_uint16_t num_entries;
  185. grub_uint8_t reserved;
  186. grub_uint8_t file_type;
  187. struct grub_udf_lb_addr parent_idb;
  188. grub_uint16_t flags;
  189. } GRUB_PACKED;
  190. struct grub_udf_file_ident
  191. {
  192. struct grub_udf_tag tag;
  193. grub_uint16_t version_num;
  194. grub_uint8_t characteristics;
  195. #define MAX_FILE_IDENT_LENGTH 256
  196. grub_uint8_t file_ident_length;
  197. struct grub_udf_long_ad icb;
  198. grub_uint16_t imp_use_length;
  199. } GRUB_PACKED;
  200. struct grub_udf_file_entry
  201. {
  202. struct grub_udf_tag tag;
  203. struct grub_udf_icbtag icbtag;
  204. grub_uint32_t uid;
  205. grub_uint32_t gid;
  206. grub_uint32_t permissions;
  207. grub_uint16_t link_count;
  208. grub_uint8_t record_format;
  209. grub_uint8_t record_display_attr;
  210. grub_uint32_t record_length;
  211. grub_uint64_t file_size;
  212. grub_uint64_t blocks_recorded;
  213. struct grub_udf_timestamp access_time;
  214. struct grub_udf_timestamp modification_time;
  215. struct grub_udf_timestamp attr_time;
  216. grub_uint32_t checkpoint;
  217. struct grub_udf_long_ad extended_attr_idb;
  218. struct grub_udf_regid imp_ident;
  219. grub_uint64_t unique_id;
  220. grub_uint32_t ext_attr_length;
  221. grub_uint32_t alloc_descs_length;
  222. grub_uint8_t ext_attr[0];
  223. } GRUB_PACKED;
  224. struct grub_udf_extended_file_entry
  225. {
  226. struct grub_udf_tag tag;
  227. struct grub_udf_icbtag icbtag;
  228. grub_uint32_t uid;
  229. grub_uint32_t gid;
  230. grub_uint32_t permissions;
  231. grub_uint16_t link_count;
  232. grub_uint8_t record_format;
  233. grub_uint8_t record_display_attr;
  234. grub_uint32_t record_length;
  235. grub_uint64_t file_size;
  236. grub_uint64_t object_size;
  237. grub_uint64_t blocks_recorded;
  238. struct grub_udf_timestamp access_time;
  239. struct grub_udf_timestamp modification_time;
  240. struct grub_udf_timestamp create_time;
  241. struct grub_udf_timestamp attr_time;
  242. grub_uint32_t checkpoint;
  243. grub_uint32_t reserved;
  244. struct grub_udf_long_ad extended_attr_icb;
  245. struct grub_udf_long_ad streamdir_icb;
  246. struct grub_udf_regid imp_ident;
  247. grub_uint64_t unique_id;
  248. grub_uint32_t ext_attr_length;
  249. grub_uint32_t alloc_descs_length;
  250. grub_uint8_t ext_attr[0];
  251. } GRUB_PACKED;
  252. struct grub_udf_vrs
  253. {
  254. grub_uint8_t type;
  255. grub_uint8_t magic[5];
  256. grub_uint8_t version;
  257. } GRUB_PACKED;
  258. struct grub_udf_avdp
  259. {
  260. struct grub_udf_tag tag;
  261. struct grub_udf_extent_ad vds;
  262. } GRUB_PACKED;
  263. struct grub_udf_pd
  264. {
  265. struct grub_udf_tag tag;
  266. grub_uint32_t seq_num;
  267. grub_uint16_t flags;
  268. grub_uint16_t part_num;
  269. struct grub_udf_regid contents;
  270. grub_uint8_t contents_use[128];
  271. grub_uint32_t access_type;
  272. grub_uint32_t start;
  273. grub_uint32_t length;
  274. } GRUB_PACKED;
  275. struct grub_udf_partmap
  276. {
  277. grub_uint8_t type;
  278. grub_uint8_t length;
  279. union
  280. {
  281. struct
  282. {
  283. grub_uint16_t seq_num;
  284. grub_uint16_t part_num;
  285. } type1;
  286. struct
  287. {
  288. grub_uint8_t ident[62];
  289. } type2;
  290. };
  291. } GRUB_PACKED;
  292. struct grub_udf_lvd
  293. {
  294. struct grub_udf_tag tag;
  295. grub_uint32_t seq_num;
  296. struct grub_udf_charspec charset;
  297. grub_uint8_t ident[128];
  298. grub_uint32_t bsize;
  299. struct grub_udf_regid domain_ident;
  300. struct grub_udf_long_ad root_fileset;
  301. grub_uint32_t map_table_length;
  302. grub_uint32_t num_part_maps;
  303. struct grub_udf_regid imp_ident;
  304. grub_uint8_t imp_use[128];
  305. struct grub_udf_extent_ad integrity_seq_ext;
  306. grub_uint8_t part_maps[1608];
  307. } GRUB_PACKED;
  308. struct grub_udf_aed
  309. {
  310. struct grub_udf_tag tag;
  311. grub_uint32_t prev_ae;
  312. grub_uint32_t ae_len;
  313. } GRUB_PACKED;
  314. struct grub_udf_data
  315. {
  316. grub_disk_t disk;
  317. struct grub_udf_lvd lvd;
  318. struct grub_udf_pd pds[GRUB_UDF_MAX_PDS];
  319. struct grub_udf_partmap *pms[GRUB_UDF_MAX_PMS];
  320. struct grub_udf_long_ad root_icb;
  321. int npd, npm, lbshift;
  322. };
  323. struct grub_fshelp_node
  324. {
  325. struct grub_udf_data *data;
  326. int part_ref;
  327. union
  328. {
  329. struct grub_udf_file_entry fe;
  330. struct grub_udf_extended_file_entry efe;
  331. char raw[0];
  332. } block;
  333. };
  334. static inline grub_size_t
  335. get_fshelp_size (struct grub_udf_data *data)
  336. {
  337. struct grub_fshelp_node *x = NULL;
  338. return sizeof (*x)
  339. + (1 << (GRUB_DISK_SECTOR_BITS
  340. + data->lbshift)) - sizeof (x->block);
  341. }
  342. static grub_dl_t my_mod;
  343. static grub_uint32_t
  344. grub_udf_get_block (struct grub_udf_data *data,
  345. grub_uint16_t part_ref, grub_uint32_t block)
  346. {
  347. part_ref = U16 (part_ref);
  348. if (part_ref >= data->npm)
  349. {
  350. grub_error (GRUB_ERR_BAD_FS, "invalid part ref");
  351. return 0;
  352. }
  353. return (U32 (data->pds[data->pms[part_ref]->type1.part_num].start)
  354. + U32 (block));
  355. }
  356. static grub_err_t
  357. grub_udf_read_icb (struct grub_udf_data *data,
  358. struct grub_udf_long_ad *icb,
  359. struct grub_fshelp_node *node)
  360. {
  361. grub_uint32_t block;
  362. block = grub_udf_get_block (data,
  363. icb->block.part_ref,
  364. icb->block.block_num);
  365. if (grub_errno)
  366. return grub_errno;
  367. if (grub_disk_read (data->disk, block << data->lbshift, 0,
  368. 1 << (GRUB_DISK_SECTOR_BITS
  369. + data->lbshift),
  370. &node->block))
  371. return grub_errno;
  372. if ((U16 (node->block.fe.tag.tag_ident) != GRUB_UDF_TAG_IDENT_FE) &&
  373. (U16 (node->block.fe.tag.tag_ident) != GRUB_UDF_TAG_IDENT_EFE))
  374. return grub_error (GRUB_ERR_BAD_FS, "invalid fe/efe descriptor");
  375. node->part_ref = icb->block.part_ref;
  376. node->data = data;
  377. return 0;
  378. }
  379. static grub_disk_addr_t
  380. grub_udf_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
  381. {
  382. char *buf = NULL;
  383. char *ptr;
  384. grub_ssize_t len;
  385. grub_disk_addr_t filebytes;
  386. switch (U16 (node->block.fe.tag.tag_ident))
  387. {
  388. case GRUB_UDF_TAG_IDENT_FE:
  389. ptr = (char *) &node->block.fe.ext_attr[0] + U32 (node->block.fe.ext_attr_length);
  390. len = U32 (node->block.fe.alloc_descs_length);
  391. break;
  392. case GRUB_UDF_TAG_IDENT_EFE:
  393. ptr = (char *) &node->block.efe.ext_attr[0] + U32 (node->block.efe.ext_attr_length);
  394. len = U32 (node->block.efe.alloc_descs_length);
  395. break;
  396. default:
  397. grub_error (GRUB_ERR_BAD_FS, "invalid file entry");
  398. return 0;
  399. }
  400. if ((U16 (node->block.fe.icbtag.flags) & GRUB_UDF_ICBTAG_FLAG_AD_MASK)
  401. == GRUB_UDF_ICBTAG_FLAG_AD_SHORT)
  402. {
  403. struct grub_udf_short_ad *ad = (struct grub_udf_short_ad *) ptr;
  404. filebytes = fileblock * U32 (node->data->lvd.bsize);
  405. while (len >= (grub_ssize_t) sizeof (struct grub_udf_short_ad))
  406. {
  407. grub_uint32_t adlen = U32 (ad->length) & 0x3fffffff;
  408. grub_uint32_t adtype = U32 (ad->length) >> 30;
  409. if (adtype == 3)
  410. {
  411. struct grub_udf_aed *extension;
  412. grub_disk_addr_t sec = grub_udf_get_block(node->data,
  413. node->part_ref,
  414. ad->position);
  415. if (!buf)
  416. {
  417. buf = grub_malloc (U32 (node->data->lvd.bsize));
  418. if (!buf)
  419. return 0;
  420. }
  421. if (grub_disk_read (node->data->disk, sec << node->data->lbshift,
  422. 0, adlen, buf))
  423. goto fail;
  424. extension = (struct grub_udf_aed *) buf;
  425. if (U16 (extension->tag.tag_ident) != GRUB_UDF_TAG_IDENT_AED)
  426. {
  427. grub_error (GRUB_ERR_BAD_FS, "invalid aed tag");
  428. goto fail;
  429. }
  430. len = U32 (extension->ae_len);
  431. ad = (struct grub_udf_short_ad *)
  432. (buf + sizeof (struct grub_udf_aed));
  433. continue;
  434. }
  435. if (filebytes < adlen)
  436. {
  437. grub_uint32_t ad_pos = ad->position;
  438. grub_free (buf);
  439. return ((U32 (ad_pos) & GRUB_UDF_EXT_MASK) ? 0 :
  440. (grub_udf_get_block (node->data, node->part_ref, ad_pos)
  441. + (filebytes >> (GRUB_DISK_SECTOR_BITS
  442. + node->data->lbshift))));
  443. }
  444. filebytes -= adlen;
  445. ad++;
  446. len -= sizeof (struct grub_udf_short_ad);
  447. }
  448. }
  449. else
  450. {
  451. struct grub_udf_long_ad *ad = (struct grub_udf_long_ad *) ptr;
  452. filebytes = fileblock * U32 (node->data->lvd.bsize);
  453. while (len >= (grub_ssize_t) sizeof (struct grub_udf_long_ad))
  454. {
  455. grub_uint32_t adlen = U32 (ad->length) & 0x3fffffff;
  456. grub_uint32_t adtype = U32 (ad->length) >> 30;
  457. if (adtype == 3)
  458. {
  459. struct grub_udf_aed *extension;
  460. grub_disk_addr_t sec = grub_udf_get_block(node->data,
  461. ad->block.part_ref,
  462. ad->block.block_num);
  463. if (!buf)
  464. {
  465. buf = grub_malloc (U32 (node->data->lvd.bsize));
  466. if (!buf)
  467. return 0;
  468. }
  469. if (grub_disk_read (node->data->disk, sec << node->data->lbshift,
  470. 0, adlen, buf))
  471. goto fail;
  472. extension = (struct grub_udf_aed *) buf;
  473. if (U16 (extension->tag.tag_ident) != GRUB_UDF_TAG_IDENT_AED)
  474. {
  475. grub_error (GRUB_ERR_BAD_FS, "invalid aed tag");
  476. goto fail;
  477. }
  478. len = U32 (extension->ae_len);
  479. ad = (struct grub_udf_long_ad *)
  480. (buf + sizeof (struct grub_udf_aed));
  481. continue;
  482. }
  483. if (filebytes < adlen)
  484. {
  485. grub_uint32_t ad_block_num = ad->block.block_num;
  486. grub_uint32_t ad_part_ref = ad->block.part_ref;
  487. grub_free (buf);
  488. return ((U32 (ad_block_num) & GRUB_UDF_EXT_MASK) ? 0 :
  489. (grub_udf_get_block (node->data, ad_part_ref,
  490. ad_block_num)
  491. + (filebytes >> (GRUB_DISK_SECTOR_BITS
  492. + node->data->lbshift))));
  493. }
  494. filebytes -= adlen;
  495. ad++;
  496. len -= sizeof (struct grub_udf_long_ad);
  497. }
  498. }
  499. fail:
  500. grub_free (buf);
  501. return 0;
  502. }
  503. static grub_ssize_t
  504. grub_udf_read_file (grub_fshelp_node_t node,
  505. grub_disk_read_hook_t read_hook, void *read_hook_data,
  506. grub_off_t pos, grub_size_t len, char *buf)
  507. {
  508. switch (U16 (node->block.fe.icbtag.flags) & GRUB_UDF_ICBTAG_FLAG_AD_MASK)
  509. {
  510. case GRUB_UDF_ICBTAG_FLAG_AD_IN_ICB:
  511. {
  512. char *ptr;
  513. ptr = ((U16 (node->block.fe.tag.tag_ident) == GRUB_UDF_TAG_IDENT_FE) ?
  514. ((char *) &node->block.fe.ext_attr[0]
  515. + U32 (node->block.fe.ext_attr_length)) :
  516. ((char *) &node->block.efe.ext_attr[0]
  517. + U32 (node->block.efe.ext_attr_length)));
  518. grub_memcpy (buf, ptr + pos, len);
  519. return len;
  520. }
  521. case GRUB_UDF_ICBTAG_FLAG_AD_EXT:
  522. grub_error (GRUB_ERR_BAD_FS, "invalid extent type");
  523. return 0;
  524. }
  525. return grub_fshelp_read_file (node->data->disk, node,
  526. read_hook, read_hook_data,
  527. pos, len, buf, grub_udf_read_block,
  528. U64 (node->block.fe.file_size),
  529. node->data->lbshift, 0);
  530. }
  531. static unsigned sblocklist[] = { 256, 512, 0 };
  532. static struct grub_udf_data *
  533. grub_udf_mount (grub_disk_t disk)
  534. {
  535. struct grub_udf_data *data = 0;
  536. struct grub_udf_fileset root_fs;
  537. unsigned *sblklist;
  538. grub_uint32_t block, vblock;
  539. int i, lbshift;
  540. data = grub_malloc (sizeof (struct grub_udf_data));
  541. if (!data)
  542. return 0;
  543. data->disk = disk;
  544. /* Search for Anchor Volume Descriptor Pointer (AVDP)
  545. * and determine logical block size. */
  546. block = 0;
  547. for (lbshift = 0; lbshift < 4; lbshift++)
  548. {
  549. for (sblklist = sblocklist; *sblklist; sblklist++)
  550. {
  551. struct grub_udf_avdp avdp;
  552. if (grub_disk_read (disk, *sblklist << lbshift, 0,
  553. sizeof (struct grub_udf_avdp), &avdp))
  554. {
  555. grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem");
  556. goto fail;
  557. }
  558. if (U16 (avdp.tag.tag_ident) == GRUB_UDF_TAG_IDENT_AVDP &&
  559. U32 (avdp.tag.tag_location) == *sblklist)
  560. {
  561. block = U32 (avdp.vds.start);
  562. break;
  563. }
  564. }
  565. if (block)
  566. break;
  567. }
  568. if (!block)
  569. {
  570. grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem");
  571. goto fail;
  572. }
  573. data->lbshift = lbshift;
  574. /* Search for Volume Recognition Sequence (VRS). */
  575. for (vblock = (32767 >> (lbshift + GRUB_DISK_SECTOR_BITS)) + 1;;
  576. vblock += (2047 >> (lbshift + GRUB_DISK_SECTOR_BITS)) + 1)
  577. {
  578. struct grub_udf_vrs vrs;
  579. if (grub_disk_read (disk, vblock << lbshift, 0,
  580. sizeof (struct grub_udf_vrs), &vrs))
  581. {
  582. grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem");
  583. goto fail;
  584. }
  585. if ((!grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_NSR03, 5)) ||
  586. (!grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_NSR02, 5)))
  587. break;
  588. if ((grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_BEA01, 5)) &&
  589. (grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_BOOT2, 5)) &&
  590. (grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_CD001, 5)) &&
  591. (grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_CDW02, 5)) &&
  592. (grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_TEA01, 5)))
  593. {
  594. grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem");
  595. goto fail;
  596. }
  597. }
  598. data->npd = data->npm = 0;
  599. /* Locate Partition Descriptor (PD) and Logical Volume Descriptor (LVD). */
  600. while (1)
  601. {
  602. struct grub_udf_tag tag;
  603. if (grub_disk_read (disk, block << lbshift, 0,
  604. sizeof (struct grub_udf_tag), &tag))
  605. {
  606. grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem");
  607. goto fail;
  608. }
  609. tag.tag_ident = U16 (tag.tag_ident);
  610. if (tag.tag_ident == GRUB_UDF_TAG_IDENT_PD)
  611. {
  612. if (data->npd >= GRUB_UDF_MAX_PDS)
  613. {
  614. grub_error (GRUB_ERR_BAD_FS, "too many PDs");
  615. goto fail;
  616. }
  617. if (grub_disk_read (disk, block << lbshift, 0,
  618. sizeof (struct grub_udf_pd),
  619. &data->pds[data->npd]))
  620. {
  621. grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem");
  622. goto fail;
  623. }
  624. data->npd++;
  625. }
  626. else if (tag.tag_ident == GRUB_UDF_TAG_IDENT_LVD)
  627. {
  628. int k;
  629. struct grub_udf_partmap *ppm;
  630. if (grub_disk_read (disk, block << lbshift, 0,
  631. sizeof (struct grub_udf_lvd),
  632. &data->lvd))
  633. {
  634. grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem");
  635. goto fail;
  636. }
  637. if (data->npm + U32 (data->lvd.num_part_maps) > GRUB_UDF_MAX_PMS)
  638. {
  639. grub_error (GRUB_ERR_BAD_FS, "too many partition maps");
  640. goto fail;
  641. }
  642. ppm = (struct grub_udf_partmap *) &data->lvd.part_maps;
  643. for (k = U32 (data->lvd.num_part_maps); k > 0; k--)
  644. {
  645. if (ppm->type != GRUB_UDF_PARTMAP_TYPE_1)
  646. {
  647. grub_error (GRUB_ERR_BAD_FS, "partmap type not supported");
  648. goto fail;
  649. }
  650. data->pms[data->npm++] = ppm;
  651. ppm = (struct grub_udf_partmap *) ((char *) ppm +
  652. U32 (ppm->length));
  653. }
  654. }
  655. else if (tag.tag_ident > GRUB_UDF_TAG_IDENT_TD)
  656. {
  657. grub_error (GRUB_ERR_BAD_FS, "invalid tag ident");
  658. goto fail;
  659. }
  660. else if (tag.tag_ident == GRUB_UDF_TAG_IDENT_TD)
  661. break;
  662. block++;
  663. }
  664. for (i = 0; i < data->npm; i++)
  665. {
  666. int j;
  667. for (j = 0; j < data->npd; j++)
  668. if (data->pms[i]->type1.part_num == data->pds[j].part_num)
  669. {
  670. data->pms[i]->type1.part_num = j;
  671. break;
  672. }
  673. if (j == data->npd)
  674. {
  675. grub_error (GRUB_ERR_BAD_FS, "can\'t find PD");
  676. goto fail;
  677. }
  678. }
  679. block = grub_udf_get_block (data,
  680. data->lvd.root_fileset.block.part_ref,
  681. data->lvd.root_fileset.block.block_num);
  682. if (grub_errno)
  683. goto fail;
  684. if (grub_disk_read (disk, block << lbshift, 0,
  685. sizeof (struct grub_udf_fileset), &root_fs))
  686. {
  687. grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem");
  688. goto fail;
  689. }
  690. if (U16 (root_fs.tag.tag_ident) != GRUB_UDF_TAG_IDENT_FSD)
  691. {
  692. grub_error (GRUB_ERR_BAD_FS, "invalid fileset descriptor");
  693. goto fail;
  694. }
  695. data->root_icb = root_fs.root_icb;
  696. return data;
  697. fail:
  698. grub_free (data);
  699. return 0;
  700. }
  701. #ifdef GRUB_UTIL
  702. grub_disk_addr_t
  703. grub_udf_get_cluster_sector (grub_disk_t disk, grub_uint64_t *sec_per_lcn)
  704. {
  705. grub_disk_addr_t ret;
  706. static struct grub_udf_data *data;
  707. data = grub_udf_mount (disk);
  708. if (!data)
  709. return 0;
  710. ret = U32 (data->pds[data->pms[0]->type1.part_num].start);
  711. *sec_per_lcn = 1ULL << data->lbshift;
  712. grub_free (data);
  713. return ret;
  714. }
  715. #endif
  716. static char *
  717. read_string (const grub_uint8_t *raw, grub_size_t sz, char *outbuf)
  718. {
  719. grub_uint16_t *utf16 = NULL;
  720. grub_size_t utf16len = 0;
  721. if (sz == 0)
  722. return NULL;
  723. if (raw[0] != 8 && raw[0] != 16)
  724. return NULL;
  725. if (raw[0] == 8)
  726. {
  727. unsigned i;
  728. utf16len = sz - 1;
  729. utf16 = grub_malloc (utf16len * sizeof (utf16[0]));
  730. if (!utf16)
  731. return NULL;
  732. for (i = 0; i < utf16len; i++)
  733. utf16[i] = raw[i + 1];
  734. }
  735. if (raw[0] == 16)
  736. {
  737. unsigned i;
  738. utf16len = (sz - 1) / 2;
  739. utf16 = grub_malloc (utf16len * sizeof (utf16[0]));
  740. if (!utf16)
  741. return NULL;
  742. for (i = 0; i < utf16len; i++)
  743. utf16[i] = (raw[2 * i + 1] << 8) | raw[2*i + 2];
  744. }
  745. if (!outbuf)
  746. outbuf = grub_malloc (utf16len * GRUB_MAX_UTF8_PER_UTF16 + 1);
  747. if (outbuf)
  748. *grub_utf16_to_utf8 ((grub_uint8_t *) outbuf, utf16, utf16len) = '\0';
  749. grub_free (utf16);
  750. return outbuf;
  751. }
  752. static int
  753. grub_udf_iterate_dir (grub_fshelp_node_t dir,
  754. grub_fshelp_iterate_dir_hook_t hook, void *hook_data)
  755. {
  756. grub_fshelp_node_t child;
  757. struct grub_udf_file_ident dirent;
  758. grub_off_t offset = 0;
  759. child = grub_malloc (get_fshelp_size (dir->data));
  760. if (!child)
  761. return 0;
  762. /* The current directory is not stored. */
  763. grub_memcpy (child, dir, get_fshelp_size (dir->data));
  764. if (hook (".", GRUB_FSHELP_DIR, child, hook_data))
  765. return 1;
  766. while (offset < U64 (dir->block.fe.file_size))
  767. {
  768. if (grub_udf_read_file (dir, 0, 0, offset, sizeof (dirent),
  769. (char *) &dirent) != sizeof (dirent))
  770. return 0;
  771. if (U16 (dirent.tag.tag_ident) != GRUB_UDF_TAG_IDENT_FID)
  772. {
  773. grub_error (GRUB_ERR_BAD_FS, "invalid fid tag");
  774. return 0;
  775. }
  776. offset += sizeof (dirent) + U16 (dirent.imp_use_length);
  777. if (!(dirent.characteristics & GRUB_UDF_FID_CHAR_DELETED))
  778. {
  779. child = grub_malloc (get_fshelp_size (dir->data));
  780. if (!child)
  781. return 0;
  782. if (grub_udf_read_icb (dir->data, &dirent.icb, child))
  783. return 0;
  784. if (dirent.characteristics & GRUB_UDF_FID_CHAR_PARENT)
  785. {
  786. /* This is the parent directory. */
  787. if (hook ("..", GRUB_FSHELP_DIR, child, hook_data))
  788. return 1;
  789. }
  790. else
  791. {
  792. enum grub_fshelp_filetype type;
  793. char *filename;
  794. grub_uint8_t raw[MAX_FILE_IDENT_LENGTH];
  795. type = ((dirent.characteristics & GRUB_UDF_FID_CHAR_DIRECTORY) ?
  796. (GRUB_FSHELP_DIR) : (GRUB_FSHELP_REG));
  797. if (child->block.fe.icbtag.file_type == GRUB_UDF_ICBTAG_TYPE_SYMLINK)
  798. type = GRUB_FSHELP_SYMLINK;
  799. if ((grub_udf_read_file (dir, 0, 0, offset,
  800. dirent.file_ident_length,
  801. (char *) raw))
  802. != dirent.file_ident_length)
  803. return 0;
  804. filename = read_string (raw, dirent.file_ident_length, 0);
  805. if (!filename)
  806. grub_print_error ();
  807. if (filename && hook (filename, type, child, hook_data))
  808. {
  809. grub_free (filename);
  810. return 1;
  811. }
  812. grub_free (filename);
  813. }
  814. }
  815. /* Align to dword boundary. */
  816. offset = (offset + dirent.file_ident_length + 3) & (~3);
  817. }
  818. return 0;
  819. }
  820. static char *
  821. grub_udf_read_symlink (grub_fshelp_node_t node)
  822. {
  823. grub_size_t sz = U64 (node->block.fe.file_size);
  824. grub_uint8_t *raw;
  825. const grub_uint8_t *ptr;
  826. char *out, *optr;
  827. if (sz < 4)
  828. return NULL;
  829. raw = grub_malloc (sz);
  830. if (!raw)
  831. return NULL;
  832. if (grub_udf_read_file (node, NULL, NULL, 0, sz, (char *) raw) < 0)
  833. {
  834. grub_free (raw);
  835. return NULL;
  836. }
  837. out = grub_malloc (sz * 2 + 1);
  838. if (!out)
  839. {
  840. grub_free (raw);
  841. return NULL;
  842. }
  843. optr = out;
  844. for (ptr = raw; ptr < raw + sz; )
  845. {
  846. grub_size_t s;
  847. if ((grub_size_t) (ptr - raw + 4) > sz)
  848. goto fail;
  849. if (!(ptr[2] == 0 && ptr[3] == 0))
  850. goto fail;
  851. s = 4 + ptr[1];
  852. if ((grub_size_t) (ptr - raw + s) > sz)
  853. goto fail;
  854. switch (*ptr)
  855. {
  856. case 1:
  857. if (ptr[1])
  858. goto fail;
  859. /* Fallthrough. */
  860. case 2:
  861. /* in 4 bytes. out: 1 byte. */
  862. optr = out;
  863. *optr++ = '/';
  864. break;
  865. case 3:
  866. /* in 4 bytes. out: 3 bytes. */
  867. if (optr != out)
  868. *optr++ = '/';
  869. *optr++ = '.';
  870. *optr++ = '.';
  871. break;
  872. case 4:
  873. /* in 4 bytes. out: 2 bytes. */
  874. if (optr != out)
  875. *optr++ = '/';
  876. *optr++ = '.';
  877. break;
  878. case 5:
  879. /* in 4 + n bytes. out, at most: 1 + 2 * n bytes. */
  880. if (optr != out)
  881. *optr++ = '/';
  882. if (!read_string (ptr + 4, s - 4, optr))
  883. goto fail;
  884. optr += grub_strlen (optr);
  885. break;
  886. default:
  887. goto fail;
  888. }
  889. ptr += s;
  890. }
  891. *optr = 0;
  892. grub_free (raw);
  893. return out;
  894. fail:
  895. grub_free (raw);
  896. grub_free (out);
  897. grub_error (GRUB_ERR_BAD_FS, "invalid symlink");
  898. return NULL;
  899. }
  900. /* Context for grub_udf_dir. */
  901. struct grub_udf_dir_ctx
  902. {
  903. grub_fs_dir_hook_t hook;
  904. void *hook_data;
  905. };
  906. /* Helper for grub_udf_dir. */
  907. static int
  908. grub_udf_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
  909. grub_fshelp_node_t node, void *data)
  910. {
  911. struct grub_udf_dir_ctx *ctx = data;
  912. struct grub_dirhook_info info;
  913. const struct grub_udf_timestamp *tstamp = NULL;
  914. grub_memset (&info, 0, sizeof (info));
  915. info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
  916. if (U16 (node->block.fe.tag.tag_ident) == GRUB_UDF_TAG_IDENT_FE)
  917. tstamp = &node->block.fe.modification_time;
  918. else if (U16 (node->block.fe.tag.tag_ident) == GRUB_UDF_TAG_IDENT_EFE)
  919. tstamp = &node->block.efe.modification_time;
  920. if (tstamp && (U16 (tstamp->type_and_timezone) & 0xf000) == 0x1000)
  921. {
  922. grub_int16_t tz;
  923. struct grub_datetime datetime;
  924. datetime.year = U16 (tstamp->year);
  925. datetime.month = tstamp->month;
  926. datetime.day = tstamp->day;
  927. datetime.hour = tstamp->hour;
  928. datetime.minute = tstamp->minute;
  929. datetime.second = tstamp->second;
  930. tz = U16 (tstamp->type_and_timezone) & 0xfff;
  931. if (tz & 0x800)
  932. tz |= 0xf000;
  933. if (tz == -2047)
  934. tz = 0;
  935. info.mtimeset = !!grub_datetime2unixtime (&datetime, &info.mtime);
  936. info.mtime -= 60 * tz;
  937. }
  938. grub_free (node);
  939. return ctx->hook (filename, &info, ctx->hook_data);
  940. }
  941. static grub_err_t
  942. grub_udf_dir (grub_device_t device, const char *path,
  943. grub_fs_dir_hook_t hook, void *hook_data)
  944. {
  945. struct grub_udf_dir_ctx ctx = { hook, hook_data };
  946. struct grub_udf_data *data = 0;
  947. struct grub_fshelp_node *rootnode = 0;
  948. struct grub_fshelp_node *foundnode = 0;
  949. grub_dl_ref (my_mod);
  950. data = grub_udf_mount (device->disk);
  951. if (!data)
  952. goto fail;
  953. rootnode = grub_malloc (get_fshelp_size (data));
  954. if (!rootnode)
  955. goto fail;
  956. if (grub_udf_read_icb (data, &data->root_icb, rootnode))
  957. goto fail;
  958. if (grub_fshelp_find_file (path, rootnode,
  959. &foundnode,
  960. grub_udf_iterate_dir, grub_udf_read_symlink,
  961. GRUB_FSHELP_DIR))
  962. goto fail;
  963. grub_udf_iterate_dir (foundnode, grub_udf_dir_iter, &ctx);
  964. if (foundnode != rootnode)
  965. grub_free (foundnode);
  966. fail:
  967. grub_free (rootnode);
  968. grub_free (data);
  969. grub_dl_unref (my_mod);
  970. return grub_errno;
  971. }
  972. static grub_err_t
  973. grub_udf_open (struct grub_file *file, const char *name)
  974. {
  975. struct grub_udf_data *data;
  976. struct grub_fshelp_node *rootnode = 0;
  977. struct grub_fshelp_node *foundnode;
  978. grub_dl_ref (my_mod);
  979. data = grub_udf_mount (file->device->disk);
  980. if (!data)
  981. goto fail;
  982. rootnode = grub_malloc (get_fshelp_size (data));
  983. if (!rootnode)
  984. goto fail;
  985. if (grub_udf_read_icb (data, &data->root_icb, rootnode))
  986. goto fail;
  987. if (grub_fshelp_find_file (name, rootnode,
  988. &foundnode,
  989. grub_udf_iterate_dir, grub_udf_read_symlink,
  990. GRUB_FSHELP_REG))
  991. goto fail;
  992. file->data = foundnode;
  993. file->offset = 0;
  994. file->size = U64 (foundnode->block.fe.file_size);
  995. grub_free (rootnode);
  996. return 0;
  997. fail:
  998. grub_dl_unref (my_mod);
  999. grub_free (data);
  1000. grub_free (rootnode);
  1001. return grub_errno;
  1002. }
  1003. static grub_ssize_t
  1004. grub_udf_read (grub_file_t file, char *buf, grub_size_t len)
  1005. {
  1006. struct grub_fshelp_node *node = (struct grub_fshelp_node *) file->data;
  1007. return grub_udf_read_file (node, file->read_hook, file->read_hook_data,
  1008. file->offset, len, buf);
  1009. }
  1010. static grub_err_t
  1011. grub_udf_close (grub_file_t file)
  1012. {
  1013. if (file->data)
  1014. {
  1015. struct grub_fshelp_node *node = (struct grub_fshelp_node *) file->data;
  1016. grub_free (node->data);
  1017. grub_free (node);
  1018. }
  1019. grub_dl_unref (my_mod);
  1020. return GRUB_ERR_NONE;
  1021. }
  1022. static grub_err_t
  1023. grub_udf_label (grub_device_t device, char **label)
  1024. {
  1025. struct grub_udf_data *data;
  1026. data = grub_udf_mount (device->disk);
  1027. if (data)
  1028. {
  1029. *label = read_string (data->lvd.ident, sizeof (data->lvd.ident), 0);
  1030. grub_free (data);
  1031. }
  1032. else
  1033. *label = 0;
  1034. return grub_errno;
  1035. }
  1036. static struct grub_fs grub_udf_fs = {
  1037. .name = "udf",
  1038. .dir = grub_udf_dir,
  1039. .open = grub_udf_open,
  1040. .read = grub_udf_read,
  1041. .close = grub_udf_close,
  1042. .label = grub_udf_label,
  1043. #ifdef GRUB_UTIL
  1044. .reserved_first_sector = 1,
  1045. .blocklist_install = 1,
  1046. #endif
  1047. .next = 0
  1048. };
  1049. GRUB_MOD_INIT (udf)
  1050. {
  1051. grub_fs_register (&grub_udf_fs);
  1052. my_mod = mod;
  1053. }
  1054. GRUB_MOD_FINI (udf)
  1055. {
  1056. grub_fs_unregister (&grub_udf_fs);
  1057. }