udf.c 36 KB

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