ufs.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. /* ufs.c - Unix File System */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2004,2005,2007,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/i18n.h>
  27. GRUB_MOD_LICENSE ("GPLv3+");
  28. #ifdef MODE_UFS2
  29. #define GRUB_UFS_MAGIC 0x19540119
  30. #else
  31. #define GRUB_UFS_MAGIC 0x11954
  32. #endif
  33. #define GRUB_UFS_INODE 2
  34. #define GRUB_UFS_FILETYPE_DIR 4
  35. #define GRUB_UFS_FILETYPE_LNK 10
  36. #define GRUB_UFS_MAX_SYMLNK_CNT 8
  37. #define GRUB_UFS_DIRBLKS 12
  38. #define GRUB_UFS_INDIRBLKS 3
  39. #define GRUB_UFS_ATTR_TYPE 0160000
  40. #define GRUB_UFS_ATTR_FILE 0100000
  41. #define GRUB_UFS_ATTR_DIR 0040000
  42. #define GRUB_UFS_ATTR_LNK 0120000
  43. #define GRUB_UFS_VOLNAME_LEN 32
  44. #ifdef MODE_BIGENDIAN
  45. #define grub_ufs_to_cpu16 grub_be_to_cpu16
  46. #define grub_ufs_to_cpu32 grub_be_to_cpu32
  47. #define grub_ufs_to_cpu64 grub_be_to_cpu64
  48. #define grub_cpu_to_ufs32_compile_time grub_cpu_to_be32_compile_time
  49. #else
  50. #define grub_ufs_to_cpu16 grub_le_to_cpu16
  51. #define grub_ufs_to_cpu32 grub_le_to_cpu32
  52. #define grub_ufs_to_cpu64 grub_le_to_cpu64
  53. #define grub_cpu_to_ufs32_compile_time grub_cpu_to_le32_compile_time
  54. #endif
  55. #ifdef MODE_UFS2
  56. typedef grub_uint64_t grub_ufs_blk_t;
  57. static inline grub_disk_addr_t
  58. grub_ufs_to_cpu_blk (grub_ufs_blk_t blk)
  59. {
  60. return grub_ufs_to_cpu64 (blk);
  61. }
  62. #else
  63. typedef grub_uint32_t grub_ufs_blk_t;
  64. static inline grub_disk_addr_t
  65. grub_ufs_to_cpu_blk (grub_ufs_blk_t blk)
  66. {
  67. return grub_ufs_to_cpu32 (blk);
  68. }
  69. #endif
  70. /* Calculate in which group the inode can be found. */
  71. #define UFS_BLKSZ(sblock) (grub_ufs_to_cpu32 (sblock->bsize))
  72. #define UFS_LOG_BLKSZ(sblock) (data->log2_blksz)
  73. #ifdef MODE_UFS2
  74. #define INODE_ENDIAN(data,field,bits1,bits2) grub_ufs_to_cpu##bits2 (data->inode.field)
  75. #else
  76. #define INODE_ENDIAN(data,field,bits1,bits2) grub_ufs_to_cpu##bits1 (data->inode.field)
  77. #endif
  78. #define INODE_SIZE(data) grub_ufs_to_cpu64 (data->inode.size)
  79. #define INODE_MODE(data) grub_ufs_to_cpu16 (data->inode.mode)
  80. #ifdef MODE_UFS2
  81. #define LOG_INODE_BLKSZ 3
  82. #else
  83. #define LOG_INODE_BLKSZ 2
  84. #endif
  85. #ifdef MODE_UFS2
  86. #define UFS_INODE_PER_BLOCK 2
  87. #else
  88. #define UFS_INODE_PER_BLOCK 4
  89. #endif
  90. #define INODE_DIRBLOCKS(data,blk) INODE_ENDIAN \
  91. (data,blocks.dir_blocks[blk],32,64)
  92. #define INODE_INDIRBLOCKS(data,blk) INODE_ENDIAN \
  93. (data,blocks.indir_blocks[blk],32,64)
  94. /* The blocks on which the superblock can be found. */
  95. static int sblocklist[] = { 128, 16, 0, 512, -1 };
  96. struct grub_ufs_sblock
  97. {
  98. grub_uint8_t unused[16];
  99. /* The offset of the inodes in the cylinder group. */
  100. grub_uint32_t inoblk_offs;
  101. grub_uint8_t unused2[4];
  102. /* The start of the cylinder group. */
  103. grub_uint32_t cylg_offset;
  104. grub_uint32_t cylg_mask;
  105. grub_uint32_t mtime;
  106. grub_uint8_t unused4[12];
  107. /* The size of a block in bytes. */
  108. grub_int32_t bsize;
  109. grub_uint8_t unused5[48];
  110. /* The size of filesystem blocks to disk blocks. */
  111. grub_uint32_t log2_blksz;
  112. grub_uint8_t unused6[40];
  113. grub_uint32_t uuidhi;
  114. grub_uint32_t uuidlow;
  115. grub_uint8_t unused7[32];
  116. /* Inodes stored per cylinder group. */
  117. grub_uint32_t ino_per_group;
  118. /* The frags per cylinder group. */
  119. grub_uint32_t frags_per_group;
  120. grub_uint8_t unused8[488];
  121. /* Volume name for UFS2. */
  122. grub_uint8_t volume_name[GRUB_UFS_VOLNAME_LEN];
  123. grub_uint8_t unused9[360];
  124. grub_uint64_t mtime2;
  125. grub_uint8_t unused10[292];
  126. /* Magic value to check if this is really a UFS filesystem. */
  127. grub_uint32_t magic;
  128. };
  129. #ifdef MODE_UFS2
  130. /* UFS inode. */
  131. struct grub_ufs_inode
  132. {
  133. grub_uint16_t mode;
  134. grub_uint16_t nlinks;
  135. grub_uint32_t uid;
  136. grub_uint32_t gid;
  137. grub_uint32_t blocksize;
  138. grub_uint64_t size;
  139. grub_int64_t nblocks;
  140. grub_uint64_t atime;
  141. grub_uint64_t mtime;
  142. grub_uint64_t ctime;
  143. grub_uint64_t create_time;
  144. grub_uint32_t atime_usec;
  145. grub_uint32_t mtime_usec;
  146. grub_uint32_t ctime_usec;
  147. grub_uint32_t create_time_sec;
  148. grub_uint32_t gen;
  149. grub_uint32_t kernel_flags;
  150. grub_uint32_t flags;
  151. grub_uint32_t extsz;
  152. grub_uint64_t ext[2];
  153. union
  154. {
  155. struct
  156. {
  157. grub_uint64_t dir_blocks[GRUB_UFS_DIRBLKS];
  158. grub_uint64_t indir_blocks[GRUB_UFS_INDIRBLKS];
  159. } blocks;
  160. grub_uint8_t symlink[(GRUB_UFS_DIRBLKS + GRUB_UFS_INDIRBLKS) * 8];
  161. };
  162. grub_uint8_t unused[24];
  163. } GRUB_PACKED;
  164. #else
  165. /* UFS inode. */
  166. struct grub_ufs_inode
  167. {
  168. grub_uint16_t mode;
  169. grub_uint16_t nlinks;
  170. grub_uint16_t uid;
  171. grub_uint16_t gid;
  172. grub_uint64_t size;
  173. grub_uint32_t atime;
  174. grub_uint32_t atime_usec;
  175. grub_uint32_t mtime;
  176. grub_uint32_t mtime_usec;
  177. grub_uint32_t ctime;
  178. grub_uint32_t ctime_usec;
  179. union
  180. {
  181. struct
  182. {
  183. grub_uint32_t dir_blocks[GRUB_UFS_DIRBLKS];
  184. grub_uint32_t indir_blocks[GRUB_UFS_INDIRBLKS];
  185. } blocks;
  186. grub_uint8_t symlink[(GRUB_UFS_DIRBLKS + GRUB_UFS_INDIRBLKS) * 4];
  187. };
  188. grub_uint32_t flags;
  189. grub_uint32_t nblocks;
  190. grub_uint32_t gen;
  191. grub_uint32_t unused;
  192. grub_uint8_t pad[12];
  193. } GRUB_PACKED;
  194. #endif
  195. /* Directory entry. */
  196. struct grub_ufs_dirent
  197. {
  198. grub_uint32_t ino;
  199. grub_uint16_t direntlen;
  200. union
  201. {
  202. grub_uint16_t namelen;
  203. struct
  204. {
  205. grub_uint8_t filetype_bsd;
  206. grub_uint8_t namelen_bsd;
  207. };
  208. };
  209. } GRUB_PACKED;
  210. /* Information about a "mounted" ufs filesystem. */
  211. struct grub_ufs_data
  212. {
  213. struct grub_ufs_sblock sblock;
  214. grub_disk_t disk;
  215. struct grub_ufs_inode inode;
  216. int ino;
  217. int linknest;
  218. int log2_blksz;
  219. };
  220. static grub_dl_t my_mod;
  221. /* Forward declaration. */
  222. static grub_err_t grub_ufs_find_file (struct grub_ufs_data *data,
  223. const char *path);
  224. static grub_disk_addr_t
  225. grub_ufs_get_file_block (struct grub_ufs_data *data, grub_disk_addr_t blk)
  226. {
  227. unsigned long indirsz;
  228. int log2_blksz, log_indirsz;
  229. /* Direct. */
  230. if (blk < GRUB_UFS_DIRBLKS)
  231. return INODE_DIRBLOCKS (data, blk);
  232. log2_blksz = grub_ufs_to_cpu32 (data->sblock.log2_blksz);
  233. blk -= GRUB_UFS_DIRBLKS;
  234. log_indirsz = data->log2_blksz - LOG_INODE_BLKSZ;
  235. indirsz = 1 << log_indirsz;
  236. /* Single indirect block. */
  237. if (blk < indirsz)
  238. {
  239. grub_ufs_blk_t indir;
  240. grub_disk_read (data->disk,
  241. ((grub_disk_addr_t) INODE_INDIRBLOCKS (data, 0))
  242. << log2_blksz,
  243. blk * sizeof (indir), sizeof (indir), &indir);
  244. return indir;
  245. }
  246. blk -= indirsz;
  247. /* Double indirect block. */
  248. if (blk < (grub_disk_addr_t) indirsz * (grub_disk_addr_t) indirsz)
  249. {
  250. grub_ufs_blk_t indir;
  251. grub_disk_read (data->disk,
  252. ((grub_disk_addr_t) INODE_INDIRBLOCKS (data, 1))
  253. << log2_blksz,
  254. (blk >> log_indirsz) * sizeof (indir),
  255. sizeof (indir), &indir);
  256. grub_disk_read (data->disk,
  257. grub_ufs_to_cpu_blk (indir) << log2_blksz,
  258. (blk & ((1 << log_indirsz) - 1)) * sizeof (indir),
  259. sizeof (indir), &indir);
  260. return indir;
  261. }
  262. blk -= (grub_disk_addr_t) indirsz * (grub_disk_addr_t) indirsz;
  263. /* Triple indirect block. */
  264. if (!(blk >> (3 * log_indirsz)))
  265. {
  266. grub_ufs_blk_t indir;
  267. grub_disk_read (data->disk,
  268. ((grub_disk_addr_t) INODE_INDIRBLOCKS (data, 2))
  269. << log2_blksz,
  270. (blk >> (2 * log_indirsz)) * sizeof (indir),
  271. sizeof (indir), &indir);
  272. grub_disk_read (data->disk,
  273. grub_ufs_to_cpu_blk (indir) << log2_blksz,
  274. ((blk >> log_indirsz)
  275. & ((1 << log_indirsz) - 1)) * sizeof (indir),
  276. sizeof (indir), &indir);
  277. grub_disk_read (data->disk,
  278. grub_ufs_to_cpu_blk (indir) << log2_blksz,
  279. (blk & ((1 << log_indirsz) - 1)) * sizeof (indir),
  280. sizeof (indir), &indir);
  281. return indir;
  282. }
  283. grub_error (GRUB_ERR_BAD_FS,
  284. "ufs does not support quadruple indirect blocks");
  285. return 0;
  286. }
  287. /* Read LEN bytes from the file described by DATA starting with byte
  288. POS. Return the amount of read bytes in READ. */
  289. static grub_ssize_t
  290. grub_ufs_read_file (struct grub_ufs_data *data,
  291. grub_disk_read_hook_t read_hook, void *read_hook_data,
  292. grub_off_t pos, grub_size_t len, char *buf)
  293. {
  294. struct grub_ufs_sblock *sblock = &data->sblock;
  295. grub_off_t i;
  296. grub_off_t blockcnt;
  297. /* Adjust len so it we can't read past the end of the file. */
  298. if (len + pos > INODE_SIZE (data))
  299. len = INODE_SIZE (data) - pos;
  300. blockcnt = (len + pos + UFS_BLKSZ (sblock) - 1) >> UFS_LOG_BLKSZ (sblock);
  301. for (i = pos >> UFS_LOG_BLKSZ (sblock); i < blockcnt; i++)
  302. {
  303. grub_disk_addr_t blknr;
  304. grub_off_t blockoff;
  305. grub_off_t blockend = UFS_BLKSZ (sblock);
  306. int skipfirst = 0;
  307. blockoff = pos & (UFS_BLKSZ (sblock) - 1);
  308. blknr = grub_ufs_get_file_block (data, i);
  309. if (grub_errno)
  310. return -1;
  311. /* Last block. */
  312. if (i == blockcnt - 1)
  313. {
  314. blockend = (len + pos) & (UFS_BLKSZ (sblock) - 1);
  315. if (!blockend)
  316. blockend = UFS_BLKSZ (sblock);
  317. }
  318. /* First block. */
  319. if (i == (pos >> UFS_LOG_BLKSZ (sblock)))
  320. {
  321. skipfirst = blockoff;
  322. blockend -= skipfirst;
  323. }
  324. /* XXX: If the block number is 0 this block is not stored on
  325. disk but is zero filled instead. */
  326. if (blknr)
  327. {
  328. data->disk->read_hook = read_hook;
  329. data->disk->read_hook_data = read_hook_data;
  330. grub_disk_read (data->disk,
  331. blknr << grub_ufs_to_cpu32 (data->sblock.log2_blksz),
  332. skipfirst, blockend, buf);
  333. data->disk->read_hook = 0;
  334. if (grub_errno)
  335. return -1;
  336. }
  337. else
  338. grub_memset (buf, 0, blockend);
  339. buf += UFS_BLKSZ (sblock) - skipfirst;
  340. }
  341. return len;
  342. }
  343. /* Read inode INO from the mounted filesystem described by DATA. This
  344. inode is used by default now. */
  345. static grub_err_t
  346. grub_ufs_read_inode (struct grub_ufs_data *data, int ino, char *inode)
  347. {
  348. struct grub_ufs_sblock *sblock = &data->sblock;
  349. /* Determine the group the inode is in. */
  350. int group = ino / grub_ufs_to_cpu32 (sblock->ino_per_group);
  351. /* Determine the inode within the group. */
  352. int grpino = ino % grub_ufs_to_cpu32 (sblock->ino_per_group);
  353. /* The first block of the group. */
  354. int grpblk = group * (grub_ufs_to_cpu32 (sblock->frags_per_group));
  355. #ifndef MODE_UFS2
  356. grpblk += grub_ufs_to_cpu32 (sblock->cylg_offset)
  357. * (group & (~grub_ufs_to_cpu32 (sblock->cylg_mask)));
  358. #endif
  359. if (!inode)
  360. {
  361. inode = (char *) &data->inode;
  362. data->ino = ino;
  363. }
  364. grub_disk_read (data->disk,
  365. ((grub_ufs_to_cpu32 (sblock->inoblk_offs) + grpblk)
  366. << grub_ufs_to_cpu32 (data->sblock.log2_blksz))
  367. + grpino / UFS_INODE_PER_BLOCK,
  368. (grpino % UFS_INODE_PER_BLOCK)
  369. * sizeof (struct grub_ufs_inode),
  370. sizeof (struct grub_ufs_inode),
  371. inode);
  372. return grub_errno;
  373. }
  374. /* Lookup the symlink the current inode points to. INO is the inode
  375. number of the directory the symlink is relative to. */
  376. static grub_err_t
  377. grub_ufs_lookup_symlink (struct grub_ufs_data *data, int ino)
  378. {
  379. char *symlink;
  380. grub_size_t sz = INODE_SIZE (data);
  381. if (++data->linknest > GRUB_UFS_MAX_SYMLNK_CNT)
  382. return grub_error (GRUB_ERR_SYMLINK_LOOP, N_("too deep nesting of symlinks"));
  383. symlink = grub_malloc (sz + 1);
  384. if (!symlink)
  385. return grub_errno;
  386. /* Normally we should just check that data->inode.nblocks == 0.
  387. However old Linux doesn't maintain nblocks correctly and so it's always
  388. 0. If size is bigger than inline space then the symlink is surely not
  389. inline. */
  390. /* Check against zero is paylindromic, no need to swap. */
  391. if (data->inode.nblocks == 0
  392. && INODE_SIZE (data) <= sizeof (data->inode.symlink))
  393. grub_strcpy (symlink, (char *) data->inode.symlink);
  394. else
  395. {
  396. if (grub_ufs_read_file (data, 0, 0, 0, sz, symlink) < 0)
  397. {
  398. grub_free(symlink);
  399. return grub_errno;
  400. }
  401. }
  402. symlink[sz] = '\0';
  403. /* The symlink is an absolute path, go back to the root inode. */
  404. if (symlink[0] == '/')
  405. ino = GRUB_UFS_INODE;
  406. /* Now load in the old inode. */
  407. if (grub_ufs_read_inode (data, ino, 0))
  408. {
  409. grub_free (symlink);
  410. return grub_errno;
  411. }
  412. grub_ufs_find_file (data, symlink);
  413. grub_free (symlink);
  414. return grub_errno;
  415. }
  416. /* Find the file with the pathname PATH on the filesystem described by
  417. DATA. */
  418. static grub_err_t
  419. grub_ufs_find_file (struct grub_ufs_data *data, const char *path)
  420. {
  421. const char *name;
  422. const char *next = path;
  423. unsigned int pos = 0;
  424. int dirino;
  425. char *filename;
  426. /* We reject filenames longer than the one we're looking
  427. for without reading, so this allocation is enough. */
  428. filename = grub_malloc (grub_strlen (path) + 2);
  429. if (!filename)
  430. return grub_errno;
  431. while (1)
  432. {
  433. struct grub_ufs_dirent dirent;
  434. name = next;
  435. /* Skip the first slash. */
  436. while (*name == '/')
  437. name++;
  438. if (*name == 0)
  439. {
  440. grub_free (filename);
  441. return GRUB_ERR_NONE;
  442. }
  443. if ((INODE_MODE(data) & GRUB_UFS_ATTR_TYPE)
  444. != GRUB_UFS_ATTR_DIR)
  445. {
  446. grub_error (GRUB_ERR_BAD_FILE_TYPE,
  447. N_("not a directory"));
  448. goto fail;
  449. }
  450. /* Extract the actual part from the pathname. */
  451. for (next = name; *next && *next != '/'; next++);
  452. for (pos = 0; ; pos += grub_ufs_to_cpu16 (dirent.direntlen))
  453. {
  454. int namelen;
  455. if (pos >= INODE_SIZE (data))
  456. {
  457. grub_error (GRUB_ERR_FILE_NOT_FOUND,
  458. N_("file `%s' not found"),
  459. path);
  460. goto fail;
  461. }
  462. if (grub_ufs_read_file (data, 0, 0, pos, sizeof (dirent),
  463. (char *) &dirent) < 0)
  464. goto fail;
  465. #ifdef MODE_UFS2
  466. namelen = dirent.namelen_bsd;
  467. #else
  468. namelen = grub_ufs_to_cpu16 (dirent.namelen);
  469. #endif
  470. if (namelen < next - name)
  471. continue;
  472. if (grub_ufs_read_file (data, 0, 0, pos + sizeof (dirent),
  473. next - name + (namelen != next - name),
  474. filename) < 0)
  475. goto fail;
  476. if (grub_strncmp (name, filename, next - name) == 0
  477. && (namelen == next - name || filename[next - name] == '\0'))
  478. {
  479. dirino = data->ino;
  480. grub_ufs_read_inode (data, grub_ufs_to_cpu32 (dirent.ino), 0);
  481. if ((INODE_MODE(data) & GRUB_UFS_ATTR_TYPE)
  482. == GRUB_UFS_ATTR_LNK)
  483. {
  484. grub_ufs_lookup_symlink (data, dirino);
  485. if (grub_errno)
  486. goto fail;
  487. }
  488. break;
  489. }
  490. }
  491. }
  492. fail:
  493. grub_free (filename);
  494. return grub_errno;
  495. }
  496. /* Mount the filesystem on the disk DISK. */
  497. static struct grub_ufs_data *
  498. grub_ufs_mount (grub_disk_t disk)
  499. {
  500. struct grub_ufs_data *data;
  501. int *sblklist = sblocklist;
  502. data = grub_malloc (sizeof (struct grub_ufs_data));
  503. if (!data)
  504. return 0;
  505. /* Find a UFS sblock. */
  506. while (*sblklist != -1)
  507. {
  508. grub_disk_read (disk, *sblklist, 0, sizeof (struct grub_ufs_sblock),
  509. &data->sblock);
  510. if (grub_errno)
  511. goto fail;
  512. /* No need to byteswap bsize in this check. It works the same on both
  513. endiannesses. */
  514. if (data->sblock.magic == grub_cpu_to_ufs32_compile_time (GRUB_UFS_MAGIC)
  515. && data->sblock.bsize != 0
  516. && ((data->sblock.bsize & (data->sblock.bsize - 1)) == 0)
  517. && data->sblock.ino_per_group != 0)
  518. {
  519. for (data->log2_blksz = 0;
  520. (1U << data->log2_blksz) < grub_ufs_to_cpu32 (data->sblock.bsize);
  521. data->log2_blksz++);
  522. data->disk = disk;
  523. data->linknest = 0;
  524. return data;
  525. }
  526. sblklist++;
  527. }
  528. fail:
  529. if (grub_errno == GRUB_ERR_NONE || grub_errno == GRUB_ERR_OUT_OF_RANGE)
  530. {
  531. #ifdef MODE_UFS2
  532. grub_error (GRUB_ERR_BAD_FS, "not an ufs2 filesystem");
  533. #else
  534. grub_error (GRUB_ERR_BAD_FS, "not an ufs1 filesystem");
  535. #endif
  536. }
  537. grub_free (data);
  538. return 0;
  539. }
  540. static grub_err_t
  541. grub_ufs_dir (grub_device_t device, const char *path,
  542. grub_fs_dir_hook_t hook, void *hook_data)
  543. {
  544. struct grub_ufs_data *data;
  545. unsigned int pos = 0;
  546. data = grub_ufs_mount (device->disk);
  547. if (!data)
  548. return grub_errno;
  549. grub_ufs_read_inode (data, GRUB_UFS_INODE, 0);
  550. if (grub_errno)
  551. return grub_errno;
  552. if (!path || path[0] != '/')
  553. {
  554. grub_error (GRUB_ERR_BAD_FILENAME, N_("invalid file name `%s'"), path);
  555. return grub_errno;
  556. }
  557. grub_ufs_find_file (data, path);
  558. if (grub_errno)
  559. goto fail;
  560. if ((INODE_MODE (data) & GRUB_UFS_ATTR_TYPE) != GRUB_UFS_ATTR_DIR)
  561. {
  562. grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("not a directory"));
  563. goto fail;
  564. }
  565. while (pos < INODE_SIZE (data))
  566. {
  567. struct grub_ufs_dirent dirent;
  568. int namelen;
  569. if (grub_ufs_read_file (data, 0, 0, pos, sizeof (dirent),
  570. (char *) &dirent) < 0)
  571. break;
  572. if (dirent.direntlen == 0)
  573. break;
  574. #ifdef MODE_UFS2
  575. namelen = dirent.namelen_bsd;
  576. #else
  577. namelen = grub_ufs_to_cpu16 (dirent.namelen);
  578. #endif
  579. char *filename = grub_malloc (namelen + 1);
  580. if (!filename)
  581. goto fail;
  582. struct grub_dirhook_info info;
  583. struct grub_ufs_inode inode;
  584. grub_memset (&info, 0, sizeof (info));
  585. if (grub_ufs_read_file (data, 0, 0, pos + sizeof (dirent),
  586. namelen, filename) < 0)
  587. {
  588. grub_free (filename);
  589. break;
  590. }
  591. filename[namelen] = '\0';
  592. grub_ufs_read_inode (data, grub_ufs_to_cpu32 (dirent.ino),
  593. (char *) &inode);
  594. info.dir = ((grub_ufs_to_cpu16 (inode.mode) & GRUB_UFS_ATTR_TYPE)
  595. == GRUB_UFS_ATTR_DIR);
  596. #ifdef MODE_UFS2
  597. info.mtime = grub_ufs_to_cpu64 (inode.mtime);
  598. #else
  599. info.mtime = grub_ufs_to_cpu32 (inode.mtime);
  600. #endif
  601. info.mtimeset = 1;
  602. if (hook (filename, &info, hook_data))
  603. {
  604. grub_free (filename);
  605. break;
  606. }
  607. grub_free (filename);
  608. pos += grub_ufs_to_cpu16 (dirent.direntlen);
  609. }
  610. fail:
  611. grub_free (data);
  612. return grub_errno;
  613. }
  614. /* Open a file named NAME and initialize FILE. */
  615. static grub_err_t
  616. grub_ufs_open (struct grub_file *file, const char *name)
  617. {
  618. struct grub_ufs_data *data;
  619. data = grub_ufs_mount (file->device->disk);
  620. if (!data)
  621. return grub_errno;
  622. grub_ufs_read_inode (data, 2, 0);
  623. if (grub_errno)
  624. {
  625. grub_free (data);
  626. return grub_errno;
  627. }
  628. if (!name || name[0] != '/')
  629. {
  630. grub_error (GRUB_ERR_BAD_FILENAME, N_("invalid file name `%s'"), name);
  631. return grub_errno;
  632. }
  633. grub_ufs_find_file (data, name);
  634. if (grub_errno)
  635. {
  636. grub_free (data);
  637. return grub_errno;
  638. }
  639. file->data = data;
  640. file->size = INODE_SIZE (data);
  641. return GRUB_ERR_NONE;
  642. }
  643. static grub_ssize_t
  644. grub_ufs_read (grub_file_t file, char *buf, grub_size_t len)
  645. {
  646. struct grub_ufs_data *data =
  647. (struct grub_ufs_data *) file->data;
  648. return grub_ufs_read_file (data, file->read_hook, file->read_hook_data,
  649. file->offset, len, buf);
  650. }
  651. static grub_err_t
  652. grub_ufs_close (grub_file_t file)
  653. {
  654. grub_free (file->data);
  655. return GRUB_ERR_NONE;
  656. }
  657. static grub_err_t
  658. grub_ufs_label (grub_device_t device, char **label)
  659. {
  660. struct grub_ufs_data *data = 0;
  661. grub_dl_ref (my_mod);
  662. *label = 0;
  663. data = grub_ufs_mount (device->disk);
  664. if (data)
  665. *label = grub_strdup ((char *) data->sblock.volume_name);
  666. grub_dl_unref (my_mod);
  667. grub_free (data);
  668. return grub_errno;
  669. }
  670. static grub_err_t
  671. grub_ufs_uuid (grub_device_t device, char **uuid)
  672. {
  673. struct grub_ufs_data *data;
  674. grub_disk_t disk = device->disk;
  675. grub_dl_ref (my_mod);
  676. data = grub_ufs_mount (disk);
  677. if (data && (data->sblock.uuidhi != 0 || data->sblock.uuidlow != 0))
  678. *uuid = grub_xasprintf ("%08x%08x",
  679. (unsigned) grub_ufs_to_cpu32 (data->sblock.uuidhi),
  680. (unsigned) grub_ufs_to_cpu32 (data->sblock.uuidlow));
  681. else
  682. *uuid = NULL;
  683. grub_dl_unref (my_mod);
  684. grub_free (data);
  685. return grub_errno;
  686. }
  687. /* Get mtime. */
  688. static grub_err_t
  689. grub_ufs_mtime (grub_device_t device, grub_int64_t *tm)
  690. {
  691. struct grub_ufs_data *data = 0;
  692. grub_dl_ref (my_mod);
  693. data = grub_ufs_mount (device->disk);
  694. if (!data)
  695. *tm = 0;
  696. else
  697. {
  698. *tm = grub_ufs_to_cpu32 (data->sblock.mtime);
  699. #ifdef MODE_UFS2
  700. if (*tm < (grub_int64_t) grub_ufs_to_cpu64 (data->sblock.mtime2))
  701. *tm = grub_ufs_to_cpu64 (data->sblock.mtime2);
  702. #endif
  703. }
  704. grub_dl_unref (my_mod);
  705. grub_free (data);
  706. return grub_errno;
  707. }
  708. static struct grub_fs grub_ufs_fs =
  709. {
  710. #ifdef MODE_UFS2
  711. .name = "ufs2",
  712. #else
  713. #ifdef MODE_BIGENDIAN
  714. .name = "ufs1_be",
  715. #else
  716. .name = "ufs1",
  717. #endif
  718. #endif
  719. .fs_dir = grub_ufs_dir,
  720. .fs_open = grub_ufs_open,
  721. .fs_read = grub_ufs_read,
  722. .fs_close = grub_ufs_close,
  723. .fs_label = grub_ufs_label,
  724. .fs_uuid = grub_ufs_uuid,
  725. .fs_mtime = grub_ufs_mtime,
  726. /* FIXME: set reserved_first_sector. */
  727. #ifdef GRUB_UTIL
  728. .blocklist_install = 1,
  729. #endif
  730. .next = 0
  731. };
  732. #ifdef MODE_UFS2
  733. GRUB_MOD_INIT(ufs2)
  734. #else
  735. #ifdef MODE_BIGENDIAN
  736. GRUB_MOD_INIT(ufs1_be)
  737. #else
  738. GRUB_MOD_INIT(ufs1)
  739. #endif
  740. #endif
  741. {
  742. grub_fs_register (&grub_ufs_fs);
  743. my_mod = mod;
  744. }
  745. #ifdef MODE_UFS2
  746. GRUB_MOD_FINI(ufs2)
  747. #else
  748. #ifdef MODE_BIGENDIAN
  749. GRUB_MOD_FINI(ufs1_be)
  750. #else
  751. GRUB_MOD_FINI(ufs1)
  752. #endif
  753. #endif
  754. {
  755. grub_fs_unregister (&grub_ufs_fs);
  756. }