minix.c 19 KB

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