minix.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. /* minix.c - The minix filesystem, version 1 and 2. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2004,2005,2006,2007,2008 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_MINIX3
  29. #define GRUB_MINIX_MAGIC 0x4D5A
  30. #elif defined(MODE_MINIX2)
  31. #define GRUB_MINIX_MAGIC 0x2468
  32. #define GRUB_MINIX_MAGIC_30 0x2478
  33. #else
  34. #define GRUB_MINIX_MAGIC 0x137F
  35. #define GRUB_MINIX_MAGIC_30 0x138F
  36. #endif
  37. #define EXT2_MAGIC 0xEF53
  38. #define GRUB_MINIX_INODE_DIR_BLOCKS 7
  39. #define GRUB_MINIX_LOG2_BSIZE 1
  40. #define GRUB_MINIX_ROOT_INODE 1
  41. #define GRUB_MINIX_MAX_SYMLNK_CNT 8
  42. #define GRUB_MINIX_SBLOCK 2
  43. #define GRUB_MINIX_IFDIR 0040000U
  44. #define GRUB_MINIX_IFLNK 0120000U
  45. #ifdef MODE_BIGENDIAN
  46. #define grub_cpu_to_minix16_compile_time grub_cpu_to_be16_compile_time
  47. #define grub_minix_to_cpu16 grub_be_to_cpu16
  48. #define grub_minix_to_cpu32 grub_be_to_cpu32
  49. #else
  50. #define grub_cpu_to_minix16_compile_time grub_cpu_to_le16_compile_time
  51. #define grub_minix_to_cpu16 grub_le_to_cpu16
  52. #define grub_minix_to_cpu32 grub_le_to_cpu32
  53. #endif
  54. #if defined(MODE_MINIX2) || defined(MODE_MINIX3)
  55. typedef grub_uint32_t grub_minix_uintn_t;
  56. #define grub_minix_to_cpu_n grub_minix_to_cpu32
  57. #else
  58. typedef grub_uint16_t grub_minix_uintn_t;
  59. #define grub_minix_to_cpu_n grub_minix_to_cpu16
  60. #endif
  61. #ifdef MODE_MINIX3
  62. typedef grub_uint32_t grub_minix_ino_t;
  63. #define grub_minix_to_cpu_ino grub_minix_to_cpu32
  64. #else
  65. typedef grub_uint16_t grub_minix_ino_t;
  66. #define grub_minix_to_cpu_ino grub_minix_to_cpu16
  67. #endif
  68. #define GRUB_MINIX_INODE_SIZE(data) (grub_minix_to_cpu32 (data->inode.size))
  69. #define GRUB_MINIX_INODE_MODE(data) (grub_minix_to_cpu16 (data->inode.mode))
  70. #define GRUB_MINIX_INODE_DIR_ZONES(data,blk) (grub_minix_to_cpu_n \
  71. (data->inode.dir_zones[blk]))
  72. #define GRUB_MINIX_INODE_INDIR_ZONE(data) (grub_minix_to_cpu_n \
  73. (data->inode.indir_zone))
  74. #define GRUB_MINIX_INODE_DINDIR_ZONE(data) (grub_minix_to_cpu_n \
  75. (data->inode.double_indir_zone))
  76. #ifdef MODE_MINIX3
  77. struct grub_minix_sblock
  78. {
  79. grub_uint32_t inode_cnt;
  80. grub_uint16_t zone_cnt;
  81. grub_uint16_t inode_bmap_size;
  82. grub_uint16_t zone_bmap_size;
  83. grub_uint16_t first_data_zone;
  84. grub_uint16_t log2_zone_size;
  85. grub_uint16_t pad;
  86. grub_uint32_t max_file_size;
  87. grub_uint32_t zones;
  88. grub_uint16_t magic;
  89. grub_uint16_t pad2;
  90. grub_uint16_t block_size;
  91. grub_uint8_t disk_version;
  92. };
  93. #else
  94. struct grub_minix_sblock
  95. {
  96. grub_uint16_t inode_cnt;
  97. grub_uint16_t zone_cnt;
  98. grub_uint16_t inode_bmap_size;
  99. grub_uint16_t zone_bmap_size;
  100. grub_uint16_t first_data_zone;
  101. grub_uint16_t log2_zone_size;
  102. grub_uint32_t max_file_size;
  103. grub_uint16_t magic;
  104. };
  105. #endif
  106. #if defined(MODE_MINIX3) || defined(MODE_MINIX2)
  107. struct grub_minix_inode
  108. {
  109. grub_uint16_t mode;
  110. grub_uint16_t nlinks;
  111. grub_uint16_t uid;
  112. grub_uint16_t gid;
  113. grub_uint32_t size;
  114. grub_uint32_t atime;
  115. grub_uint32_t mtime;
  116. grub_uint32_t ctime;
  117. grub_uint32_t dir_zones[7];
  118. grub_uint32_t indir_zone;
  119. grub_uint32_t double_indir_zone;
  120. grub_uint32_t triple_indir_zone;
  121. };
  122. #else
  123. struct grub_minix_inode
  124. {
  125. grub_uint16_t mode;
  126. grub_uint16_t uid;
  127. grub_uint32_t size;
  128. grub_uint32_t mtime;
  129. grub_uint8_t gid;
  130. grub_uint8_t nlinks;
  131. grub_uint16_t dir_zones[7];
  132. grub_uint16_t indir_zone;
  133. grub_uint16_t double_indir_zone;
  134. };
  135. #endif
  136. #if defined(MODE_MINIX3)
  137. #define MAX_MINIX_FILENAME_SIZE 60
  138. #else
  139. #define MAX_MINIX_FILENAME_SIZE 30
  140. #endif
  141. /* Information about a "mounted" minix filesystem. */
  142. struct grub_minix_data
  143. {
  144. struct grub_minix_sblock sblock;
  145. struct grub_minix_inode inode;
  146. grub_uint32_t block_per_zone;
  147. grub_minix_ino_t ino;
  148. int linknest;
  149. grub_disk_t disk;
  150. int filename_size;
  151. grub_size_t block_size;
  152. };
  153. static grub_dl_t my_mod;
  154. static grub_err_t grub_minix_find_file (struct grub_minix_data *data,
  155. const char *path);
  156. #ifdef MODE_MINIX3
  157. static inline grub_disk_addr_t
  158. grub_minix_zone2sect (struct grub_minix_data *data, grub_minix_uintn_t zone)
  159. {
  160. return ((grub_disk_addr_t) zone) * data->block_size;
  161. }
  162. #else
  163. static inline grub_disk_addr_t
  164. grub_minix_zone2sect (struct grub_minix_data *data, grub_minix_uintn_t zone)
  165. {
  166. int log2_zonesz = (GRUB_MINIX_LOG2_BSIZE
  167. + grub_minix_to_cpu16 (data->sblock.log2_zone_size));
  168. return (((grub_disk_addr_t) zone) << log2_zonesz);
  169. }
  170. #endif
  171. /* Read the block pointer in ZONE, on the offset NUM. */
  172. static grub_minix_uintn_t
  173. grub_get_indir (struct grub_minix_data *data,
  174. grub_minix_uintn_t zone,
  175. grub_minix_uintn_t num)
  176. {
  177. grub_minix_uintn_t indirn;
  178. grub_disk_read (data->disk,
  179. grub_minix_zone2sect(data, zone),
  180. sizeof (grub_minix_uintn_t) * num,
  181. sizeof (grub_minix_uintn_t), (char *) &indirn);
  182. return grub_minix_to_cpu_n (indirn);
  183. }
  184. static grub_minix_uintn_t
  185. grub_minix_get_file_block (struct grub_minix_data *data, unsigned int blk)
  186. {
  187. grub_minix_uintn_t indir;
  188. /* Direct block. */
  189. if (blk < GRUB_MINIX_INODE_DIR_BLOCKS)
  190. return GRUB_MINIX_INODE_DIR_ZONES (data, blk);
  191. /* Indirect block. */
  192. blk -= GRUB_MINIX_INODE_DIR_BLOCKS;
  193. if (blk < data->block_per_zone)
  194. {
  195. indir = grub_get_indir (data, GRUB_MINIX_INODE_INDIR_ZONE (data), blk);
  196. return indir;
  197. }
  198. /* Double indirect block. */
  199. blk -= data->block_per_zone;
  200. if (blk < (grub_uint64_t) data->block_per_zone * (grub_uint64_t) data->block_per_zone)
  201. {
  202. indir = grub_get_indir (data, GRUB_MINIX_INODE_DINDIR_ZONE (data),
  203. blk / data->block_per_zone);
  204. indir = grub_get_indir (data, indir, blk % data->block_per_zone);
  205. return indir;
  206. }
  207. #if defined (MODE_MINIX3) || defined (MODE_MINIX2)
  208. blk -= data->block_per_zone * data->block_per_zone;
  209. if (blk < ((grub_uint64_t) data->block_per_zone * (grub_uint64_t) data->block_per_zone
  210. * (grub_uint64_t) data->block_per_zone))
  211. {
  212. indir = grub_get_indir (data, grub_minix_to_cpu_n (data->inode.triple_indir_zone),
  213. (blk / data->block_per_zone) / data->block_per_zone);
  214. indir = grub_get_indir (data, indir, (blk / data->block_per_zone) % data->block_per_zone);
  215. indir = grub_get_indir (data, indir, blk % data->block_per_zone);
  216. return indir;
  217. }
  218. #endif
  219. /* This should never happen. */
  220. grub_error (GRUB_ERR_OUT_OF_RANGE, "file bigger than maximum size");
  221. return 0;
  222. }
  223. /* Read LEN bytes from the file described by DATA starting with byte
  224. POS. Return the amount of read bytes in READ. */
  225. static grub_ssize_t
  226. grub_minix_read_file (struct grub_minix_data *data,
  227. grub_disk_read_hook_t read_hook, void *read_hook_data,
  228. grub_off_t pos, grub_size_t len, char *buf)
  229. {
  230. grub_uint32_t i;
  231. grub_uint32_t blockcnt;
  232. grub_uint32_t posblock;
  233. grub_uint32_t blockoff;
  234. if (pos > GRUB_MINIX_INODE_SIZE (data))
  235. {
  236. grub_error (GRUB_ERR_OUT_OF_RANGE,
  237. N_("attempt to read past the end of file"));
  238. return -1;
  239. }
  240. /* Adjust len so it we can't read past the end of the file. */
  241. if (len + pos > GRUB_MINIX_INODE_SIZE (data))
  242. len = GRUB_MINIX_INODE_SIZE (data) - pos;
  243. if (len == 0)
  244. return 0;
  245. /* Files are at most 2G/4G - 1 bytes on minixfs. Avoid 64-bit division. */
  246. blockcnt = ((grub_uint32_t) ((len + pos - 1)
  247. >> GRUB_DISK_SECTOR_BITS)) / data->block_size + 1;
  248. posblock = (((grub_uint32_t) pos)
  249. / (data->block_size << GRUB_DISK_SECTOR_BITS));
  250. blockoff = (((grub_uint32_t) pos)
  251. % (data->block_size << GRUB_DISK_SECTOR_BITS));
  252. for (i = posblock; i < blockcnt; i++)
  253. {
  254. grub_minix_uintn_t blknr;
  255. grub_uint64_t blockend = data->block_size << GRUB_DISK_SECTOR_BITS;
  256. grub_off_t skipfirst = 0;
  257. blknr = grub_minix_get_file_block (data, i);
  258. if (grub_errno)
  259. return -1;
  260. /* Last block. */
  261. if (i == blockcnt - 1)
  262. {
  263. /* len + pos < 4G (checked above), so it doesn't overflow. */
  264. blockend = (((grub_uint32_t) (len + pos))
  265. % (data->block_size << GRUB_DISK_SECTOR_BITS));
  266. if (!blockend)
  267. blockend = data->block_size << GRUB_DISK_SECTOR_BITS;
  268. }
  269. /* First block. */
  270. if (i == posblock)
  271. {
  272. skipfirst = blockoff;
  273. blockend -= skipfirst;
  274. }
  275. data->disk->read_hook = read_hook;
  276. data->disk->read_hook_data = read_hook_data;
  277. grub_disk_read (data->disk,
  278. grub_minix_zone2sect(data, blknr),
  279. skipfirst, blockend, buf);
  280. data->disk->read_hook = 0;
  281. if (grub_errno)
  282. return -1;
  283. buf += (data->block_size << GRUB_DISK_SECTOR_BITS) - skipfirst;
  284. }
  285. return len;
  286. }
  287. /* Read inode INO from the mounted filesystem described by DATA. This
  288. inode is used by default now. */
  289. static grub_err_t
  290. grub_minix_read_inode (struct grub_minix_data *data, grub_minix_ino_t ino)
  291. {
  292. struct grub_minix_sblock *sblock = &data->sblock;
  293. /* Block in which the inode is stored. */
  294. grub_disk_addr_t block;
  295. data->ino = ino;
  296. /* The first inode in minix is inode 1. */
  297. ino--;
  298. block = grub_minix_zone2sect (data,
  299. 2 + grub_minix_to_cpu16 (sblock->inode_bmap_size)
  300. + grub_minix_to_cpu16 (sblock->zone_bmap_size));
  301. block += ino / (GRUB_DISK_SECTOR_SIZE / sizeof (struct grub_minix_inode));
  302. int offs = (ino % (GRUB_DISK_SECTOR_SIZE
  303. / sizeof (struct grub_minix_inode))
  304. * sizeof (struct grub_minix_inode));
  305. grub_disk_read (data->disk, block, offs,
  306. sizeof (struct grub_minix_inode), &data->inode);
  307. return GRUB_ERR_NONE;
  308. }
  309. /* Lookup the symlink the current inode points to. INO is the inode
  310. number of the directory the symlink is relative to. */
  311. static grub_err_t
  312. grub_minix_lookup_symlink (struct grub_minix_data *data, grub_minix_ino_t ino)
  313. {
  314. char *symlink;
  315. grub_size_t sz = GRUB_MINIX_INODE_SIZE (data);
  316. if (++data->linknest > GRUB_MINIX_MAX_SYMLNK_CNT)
  317. return grub_error (GRUB_ERR_SYMLINK_LOOP, N_("too deep nesting of symlinks"));
  318. symlink = grub_malloc (sz + 1);
  319. if (!symlink)
  320. return grub_errno;
  321. if (grub_minix_read_file (data, 0, 0, 0, sz, symlink) < 0)
  322. goto fail;
  323. symlink[sz] = '\0';
  324. /* The symlink is an absolute path, go back to the root inode. */
  325. if (symlink[0] == '/')
  326. ino = GRUB_MINIX_ROOT_INODE;
  327. /* Now load in the old inode. */
  328. if (grub_minix_read_inode (data, ino))
  329. goto fail;
  330. grub_minix_find_file (data, symlink);
  331. fail:
  332. grub_free(symlink);
  333. return grub_errno;
  334. }
  335. /* Find the file with the pathname PATH on the filesystem described by
  336. DATA. */
  337. static grub_err_t
  338. grub_minix_find_file (struct grub_minix_data *data, const char *path)
  339. {
  340. const char *name;
  341. const char *next = path;
  342. unsigned int pos = 0;
  343. grub_minix_ino_t dirino;
  344. while (1)
  345. {
  346. name = next;
  347. /* Skip the first slash. */
  348. while (*name == '/')
  349. name++;
  350. if (!*name)
  351. return GRUB_ERR_NONE;
  352. if ((GRUB_MINIX_INODE_MODE (data)
  353. & GRUB_MINIX_IFDIR) != GRUB_MINIX_IFDIR)
  354. return grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("not a directory"));
  355. /* Extract the actual part from the pathname. */
  356. for (next = name; *next && *next != '/'; next++);
  357. for (pos = 0; ; )
  358. {
  359. grub_minix_ino_t ino;
  360. char filename[MAX_MINIX_FILENAME_SIZE + 1];
  361. if (pos >= GRUB_MINIX_INODE_SIZE (data))
  362. {
  363. grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("file `%s' not found"), path);
  364. return grub_errno;
  365. }
  366. if (grub_minix_read_file (data, 0, 0, pos, sizeof (ino),
  367. (char *) &ino) < 0)
  368. return grub_errno;
  369. if (grub_minix_read_file (data, 0, 0, pos + sizeof (ino),
  370. data->filename_size, (char *) filename)< 0)
  371. return grub_errno;
  372. pos += sizeof (ino) + data->filename_size;
  373. filename[data->filename_size] = '\0';
  374. /* Check if the current direntry matches the current part of the
  375. pathname. */
  376. if (grub_strncmp (name, filename, next - name) == 0
  377. && filename[next - name] == '\0')
  378. {
  379. dirino = data->ino;
  380. grub_minix_read_inode (data, grub_minix_to_cpu_ino (ino));
  381. /* Follow the symlink. */
  382. if ((GRUB_MINIX_INODE_MODE (data)
  383. & GRUB_MINIX_IFLNK) == GRUB_MINIX_IFLNK)
  384. {
  385. grub_minix_lookup_symlink (data, dirino);
  386. if (grub_errno)
  387. return grub_errno;
  388. }
  389. break;
  390. }
  391. }
  392. }
  393. }
  394. /* Mount the filesystem on the disk DISK. */
  395. static struct grub_minix_data *
  396. grub_minix_mount (grub_disk_t disk)
  397. {
  398. struct grub_minix_data *data = NULL;
  399. grub_uint16_t ext2_marker;
  400. grub_disk_read (disk, 2, 56, sizeof (ext2_marker), &ext2_marker);
  401. if (grub_errno != GRUB_ERR_NONE)
  402. goto fail;
  403. /*
  404. * The ext2 filesystems can sometimes be mistakenly identified as MINIX, e.g.
  405. * due to the number of free ext2 inodes being written to the same location
  406. * where the MINIX superblock magic is found. Avoid such situations by
  407. * skipping any filesystems that have the ext2 superblock magic.
  408. */
  409. if (ext2_marker == grub_cpu_to_le16_compile_time (EXT2_MAGIC))
  410. goto fail;
  411. data = grub_malloc (sizeof (struct grub_minix_data));
  412. if (!data)
  413. return 0;
  414. /* Read the superblock. */
  415. grub_disk_read (disk, GRUB_MINIX_SBLOCK, 0,
  416. sizeof (struct grub_minix_sblock),&data->sblock);
  417. if (grub_errno)
  418. goto fail;
  419. if (data->sblock.magic == grub_cpu_to_minix16_compile_time (GRUB_MINIX_MAGIC))
  420. {
  421. #if !defined(MODE_MINIX3)
  422. data->filename_size = 14;
  423. #else
  424. data->filename_size = 60;
  425. #endif
  426. }
  427. #if !defined(MODE_MINIX3)
  428. else if (data->sblock.magic
  429. == grub_cpu_to_minix16_compile_time (GRUB_MINIX_MAGIC_30))
  430. data->filename_size = 30;
  431. #endif
  432. else
  433. goto fail;
  434. /* 20 means 1G zones. We could support up to 31 but already 1G isn't
  435. supported by anything else. */
  436. if (grub_minix_to_cpu16 (data->sblock.log2_zone_size) >= 20)
  437. goto fail;
  438. data->disk = disk;
  439. data->linknest = 0;
  440. #ifdef MODE_MINIX3
  441. /* These tests are endian-independent. No need to byteswap. */
  442. if (data->sblock.block_size == 0xffff)
  443. data->block_size = 2;
  444. else
  445. {
  446. if ((data->sblock.block_size == grub_cpu_to_minix16_compile_time (0x200))
  447. || (data->sblock.block_size == 0)
  448. || (data->sblock.block_size & grub_cpu_to_minix16_compile_time (0x1ff)))
  449. goto fail;
  450. data->block_size = grub_minix_to_cpu16 (data->sblock.block_size)
  451. >> GRUB_DISK_SECTOR_BITS;
  452. }
  453. #else
  454. data->block_size = 2;
  455. #endif
  456. data->block_per_zone = (((grub_uint64_t) data->block_size << \
  457. (GRUB_DISK_SECTOR_BITS + grub_minix_to_cpu16 (data->sblock.log2_zone_size)))
  458. / sizeof (grub_minix_uintn_t));
  459. if (!data->block_per_zone)
  460. goto fail;
  461. return data;
  462. fail:
  463. grub_free (data);
  464. #if defined(MODE_MINIX3)
  465. grub_error (GRUB_ERR_BAD_FS, "not a minix3 filesystem");
  466. #elif defined(MODE_MINIX2)
  467. grub_error (GRUB_ERR_BAD_FS, "not a minix2 filesystem");
  468. #else
  469. grub_error (GRUB_ERR_BAD_FS, "not a minix filesystem");
  470. #endif
  471. return 0;
  472. }
  473. static grub_err_t
  474. grub_minix_dir (grub_device_t device, const char *path,
  475. grub_fs_dir_hook_t hook, void *hook_data)
  476. {
  477. struct grub_minix_data *data = 0;
  478. unsigned int pos = 0;
  479. data = grub_minix_mount (device->disk);
  480. if (!data)
  481. return grub_errno;
  482. grub_minix_read_inode (data, GRUB_MINIX_ROOT_INODE);
  483. if (grub_errno)
  484. goto fail;
  485. grub_minix_find_file (data, path);
  486. if (grub_errno)
  487. goto fail;
  488. if ((GRUB_MINIX_INODE_MODE (data) & GRUB_MINIX_IFDIR) != GRUB_MINIX_IFDIR)
  489. {
  490. grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("not a directory"));
  491. goto fail;
  492. }
  493. while (pos < GRUB_MINIX_INODE_SIZE (data))
  494. {
  495. grub_minix_ino_t ino;
  496. char filename[MAX_MINIX_FILENAME_SIZE + 1];
  497. grub_minix_ino_t dirino = data->ino;
  498. struct grub_dirhook_info info;
  499. grub_memset (&info, 0, sizeof (info));
  500. if (grub_minix_read_file (data, 0, 0, pos, sizeof (ino),
  501. (char *) &ino) < 0)
  502. return grub_errno;
  503. if (grub_minix_read_file (data, 0, 0, pos + sizeof (ino),
  504. data->filename_size,
  505. (char *) filename) < 0)
  506. return grub_errno;
  507. filename[data->filename_size] = '\0';
  508. if (!ino)
  509. {
  510. pos += sizeof (ino) + data->filename_size;
  511. continue;
  512. }
  513. grub_minix_read_inode (data, grub_minix_to_cpu_ino (ino));
  514. info.dir = ((GRUB_MINIX_INODE_MODE (data)
  515. & GRUB_MINIX_IFDIR) == GRUB_MINIX_IFDIR);
  516. info.mtimeset = 1;
  517. info.mtime = grub_minix_to_cpu32 (data->inode.mtime);
  518. if (hook (filename, &info, hook_data) ? 1 : 0)
  519. break;
  520. /* Load the old inode back in. */
  521. grub_minix_read_inode (data, dirino);
  522. pos += sizeof (ino) + data->filename_size;
  523. }
  524. fail:
  525. grub_free (data);
  526. return grub_errno;
  527. }
  528. /* Open a file named NAME and initialize FILE. */
  529. static grub_err_t
  530. grub_minix_open (struct grub_file *file, const char *name)
  531. {
  532. struct grub_minix_data *data;
  533. data = grub_minix_mount (file->device->disk);
  534. if (!data)
  535. return grub_errno;
  536. /* Open the inode op the root directory. */
  537. grub_minix_read_inode (data, GRUB_MINIX_ROOT_INODE);
  538. if (grub_errno)
  539. {
  540. grub_free (data);
  541. return grub_errno;
  542. }
  543. if (!name || name[0] != '/')
  544. {
  545. grub_error (GRUB_ERR_BAD_FILENAME, N_("invalid file name `%s'"), name);
  546. return grub_errno;
  547. }
  548. /* Traverse the directory tree to the node that should be
  549. opened. */
  550. grub_minix_find_file (data, name);
  551. if (grub_errno)
  552. {
  553. grub_free (data);
  554. return grub_errno;
  555. }
  556. file->data = data;
  557. file->size = GRUB_MINIX_INODE_SIZE (data);
  558. return GRUB_ERR_NONE;
  559. }
  560. static grub_ssize_t
  561. grub_minix_read (grub_file_t file, char *buf, grub_size_t len)
  562. {
  563. struct grub_minix_data *data =
  564. (struct grub_minix_data *) file->data;
  565. return grub_minix_read_file (data, file->read_hook, file->read_hook_data,
  566. file->offset, len, buf);
  567. }
  568. static grub_err_t
  569. grub_minix_close (grub_file_t file)
  570. {
  571. grub_free (file->data);
  572. return GRUB_ERR_NONE;
  573. }
  574. static struct grub_fs grub_minix_fs =
  575. {
  576. #ifdef MODE_BIGENDIAN
  577. #if defined(MODE_MINIX3)
  578. .name = "minix3_be",
  579. #elif defined(MODE_MINIX2)
  580. .name = "minix2_be",
  581. #else
  582. .name = "minix_be",
  583. #endif
  584. #else
  585. #if defined(MODE_MINIX3)
  586. .name = "minix3",
  587. #elif defined(MODE_MINIX2)
  588. .name = "minix2",
  589. #else
  590. .name = "minix",
  591. #endif
  592. #endif
  593. .fs_dir = grub_minix_dir,
  594. .fs_open = grub_minix_open,
  595. .fs_read = grub_minix_read,
  596. .fs_close = grub_minix_close,
  597. #ifdef GRUB_UTIL
  598. .reserved_first_sector = 1,
  599. .blocklist_install = 1,
  600. #endif
  601. .next = 0
  602. };
  603. #ifdef MODE_BIGENDIAN
  604. #if defined(MODE_MINIX3)
  605. GRUB_MOD_INIT(minix3_be)
  606. #elif defined(MODE_MINIX2)
  607. GRUB_MOD_INIT(minix2_be)
  608. #else
  609. GRUB_MOD_INIT(minix_be)
  610. #endif
  611. #else
  612. #if defined(MODE_MINIX3)
  613. GRUB_MOD_INIT(minix3)
  614. #elif defined(MODE_MINIX2)
  615. GRUB_MOD_INIT(minix2)
  616. #else
  617. GRUB_MOD_INIT(minix)
  618. #endif
  619. #endif
  620. {
  621. grub_fs_register (&grub_minix_fs);
  622. my_mod = mod;
  623. }
  624. #ifdef MODE_BIGENDIAN
  625. #if defined(MODE_MINIX3)
  626. GRUB_MOD_FINI(minix3_be)
  627. #elif defined(MODE_MINIX2)
  628. GRUB_MOD_FINI(minix2_be)
  629. #else
  630. GRUB_MOD_FINI(minix_be)
  631. #endif
  632. #else
  633. #if defined(MODE_MINIX3)
  634. GRUB_MOD_FINI(minix3)
  635. #elif defined(MODE_MINIX2)
  636. GRUB_MOD_FINI(minix2)
  637. #else
  638. GRUB_MOD_FINI(minix)
  639. #endif
  640. #endif
  641. {
  642. grub_fs_unregister (&grub_minix_fs);
  643. }