minix.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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. #define GRUB_MINIX_MAGIC 0x137F
  27. #define GRUB_MINIX2_MAGIC 0x2468
  28. #define GRUB_MINIX_MAGIC_30 0x138F
  29. #define GRUB_MINIX2_MAGIC_30 0x2478
  30. #define GRUB_MINIX_BSIZE 1024U
  31. #define GRUB_MINIX_LOG2_BSIZE 1
  32. #define GRUB_MINIX_ROOT_INODE 1
  33. #define GRUB_MINIX_MAX_SYMLNK_CNT 8
  34. #define GRUB_MINIX_SBLOCK 2
  35. #define GRUB_MINIX_IFDIR 0040000U
  36. #define GRUB_MINIX_IFLNK 0120000U
  37. #define GRUB_MINIX_INODE(data,field) (data->version == 1 ? \
  38. data->inode. field : data->inode2. field)
  39. #define GRUB_MINIX_INODE_ENDIAN(data,field,bits1,bits2) (data->version == 1 ? \
  40. grub_le_to_cpu##bits1 (data->inode.field) : \
  41. grub_le_to_cpu##bits2 (data->inode2.field))
  42. #define GRUB_MINIX_INODE_SIZE(data) GRUB_MINIX_INODE_ENDIAN (data,size,16,32)
  43. #define GRUB_MINIX_INODE_MODE(data) GRUB_MINIX_INODE_ENDIAN (data,mode,16,16)
  44. #define GRUB_MINIX_INODE_DIR_ZONES(data,blk) GRUB_MINIX_INODE_ENDIAN \
  45. (data,dir_zones[blk],16,32)
  46. #define GRUB_MINIX_INODE_INDIR_ZONE(data) \
  47. GRUB_MINIX_INODE_ENDIAN (data,indir_zone,16,32)
  48. #define GRUB_MINIX_INODE_DINDIR_ZONE(data) \
  49. GRUB_MINIX_INODE_ENDIAN (data,double_indir_zone,16,32)
  50. #define GRUB_MINIX_INODE_BLKSZ(data) (data->version == 1 ? 2 : 4)
  51. #define GRUB_MINIX_LOG2_ZONESZ (GRUB_MINIX_LOG2_BSIZE \
  52. + grub_le_to_cpu16 (sblock->log2_zone_size))
  53. #define GRUB_MINIX_ZONESZ (GRUB_MINIX_BSIZE \
  54. << grub_le_to_cpu16 (sblock->log2_zone_size))
  55. struct grub_minix_sblock
  56. {
  57. grub_uint16_t inode_cnt;
  58. grub_uint16_t zone_cnt;
  59. grub_uint16_t inode_bmap_size;
  60. grub_uint16_t zone_bmap_size;
  61. grub_uint16_t first_data_zone;
  62. grub_uint16_t log2_zone_size;
  63. grub_uint32_t max_file_size;
  64. grub_uint16_t magic;
  65. };
  66. struct grub_minix_inode
  67. {
  68. grub_uint16_t mode;
  69. grub_uint16_t uid;
  70. grub_uint16_t size;
  71. grub_uint32_t ctime;
  72. grub_uint8_t gid;
  73. grub_uint8_t nlinks;
  74. grub_uint16_t dir_zones[7];
  75. grub_uint16_t indir_zone;
  76. grub_uint16_t double_indir_zone;
  77. };
  78. struct grub_minix2_inode
  79. {
  80. grub_uint16_t mode;
  81. grub_uint16_t nlinks;
  82. grub_uint16_t uid;
  83. grub_uint16_t gid;
  84. grub_uint32_t size;
  85. grub_uint32_t atime;
  86. grub_uint32_t mtime;
  87. grub_uint32_t ctime;
  88. grub_uint32_t dir_zones[7];
  89. grub_uint32_t indir_zone;
  90. grub_uint32_t double_indir_zone;
  91. grub_uint32_t unused;
  92. };
  93. /* Information about a "mounted" minix filesystem. */
  94. struct grub_minix_data
  95. {
  96. struct grub_minix_sblock sblock;
  97. struct grub_minix_inode inode;
  98. struct grub_minix2_inode inode2;
  99. int ino;
  100. int linknest;
  101. grub_disk_t disk;
  102. int version;
  103. int filename_size;
  104. };
  105. static grub_dl_t my_mod;
  106. static grub_err_t grub_minix_find_file (struct grub_minix_data *data,
  107. const char *path);
  108. static int
  109. grub_minix_get_file_block (struct grub_minix_data *data, unsigned int blk)
  110. {
  111. struct grub_minix_sblock *sblock = &data->sblock;
  112. int indir;
  113. auto int grub_get_indir (int, int);
  114. /* Read the block pointer in ZONE, on the offset NUM. */
  115. int grub_get_indir (int zone, int num)
  116. {
  117. if (data->version == 1)
  118. {
  119. grub_uint16_t indir16;
  120. grub_disk_read (data->disk,
  121. zone << GRUB_MINIX_LOG2_ZONESZ,
  122. sizeof (grub_uint16_t) * num,
  123. sizeof (grub_uint16_t), (char *) &indir16);
  124. return grub_le_to_cpu16 (indir16);
  125. }
  126. else
  127. {
  128. grub_uint32_t indir32;
  129. grub_disk_read (data->disk,
  130. zone << GRUB_MINIX_LOG2_ZONESZ,
  131. sizeof (grub_uint32_t) * num,
  132. sizeof (grub_uint32_t), (char *) &indir32);
  133. return grub_le_to_cpu32 (indir32);
  134. }
  135. }
  136. /* Direct block. */
  137. if (blk < 7)
  138. return GRUB_MINIX_INODE_DIR_ZONES (data, blk);
  139. /* Indirect block. */
  140. blk -= 7;
  141. if (blk < GRUB_MINIX_ZONESZ / GRUB_MINIX_INODE_BLKSZ (data))
  142. {
  143. indir = grub_get_indir (GRUB_MINIX_INODE_INDIR_ZONE (data), blk);
  144. return indir;
  145. }
  146. /* Double indirect block. */
  147. blk -= GRUB_MINIX_ZONESZ / GRUB_MINIX_INODE_BLKSZ (data);
  148. if (blk < (GRUB_MINIX_ZONESZ / GRUB_MINIX_INODE_BLKSZ (data))
  149. * (GRUB_MINIX_ZONESZ / GRUB_MINIX_INODE_BLKSZ (data)))
  150. {
  151. indir = grub_get_indir (GRUB_MINIX_INODE_DINDIR_ZONE (data),
  152. blk / GRUB_MINIX_ZONESZ);
  153. indir = grub_get_indir (indir, blk % GRUB_MINIX_ZONESZ);
  154. return indir;
  155. }
  156. /* This should never happen. */
  157. grub_error (GRUB_ERR_OUT_OF_RANGE, "file bigger than maximum size");
  158. return 0;
  159. }
  160. /* Read LEN bytes from the file described by DATA starting with byte
  161. POS. Return the amount of read bytes in READ. */
  162. static grub_ssize_t
  163. grub_minix_read_file (struct grub_minix_data *data,
  164. void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t sector,
  165. unsigned offset, unsigned length),
  166. int pos, grub_disk_addr_t len, char *buf)
  167. {
  168. struct grub_minix_sblock *sblock = &data->sblock;
  169. int i;
  170. int blockcnt;
  171. /* Adjust len so it we can't read past the end of the file. */
  172. if (len + pos > GRUB_MINIX_INODE_SIZE (data))
  173. len = GRUB_MINIX_INODE_SIZE (data) - pos;
  174. blockcnt = (len + pos + GRUB_MINIX_BSIZE - 1) / GRUB_MINIX_BSIZE;
  175. for (i = pos / GRUB_MINIX_BSIZE; i < blockcnt; i++)
  176. {
  177. int blknr;
  178. int blockoff = pos % GRUB_MINIX_BSIZE;
  179. int blockend = GRUB_MINIX_BSIZE;
  180. int skipfirst = 0;
  181. blknr = grub_minix_get_file_block (data, i);
  182. if (grub_errno)
  183. return -1;
  184. /* Last block. */
  185. if (i == blockcnt - 1)
  186. {
  187. blockend = (len + pos) % GRUB_MINIX_BSIZE;
  188. if (!blockend)
  189. blockend = GRUB_MINIX_BSIZE;
  190. }
  191. /* First block. */
  192. if (i == (pos / (int) GRUB_MINIX_BSIZE))
  193. {
  194. skipfirst = blockoff;
  195. blockend -= skipfirst;
  196. }
  197. data->disk->read_hook = read_hook;
  198. grub_disk_read (data->disk, blknr << GRUB_MINIX_LOG2_ZONESZ,
  199. skipfirst, blockend, buf);
  200. data->disk->read_hook = 0;
  201. if (grub_errno)
  202. return -1;
  203. buf += GRUB_MINIX_BSIZE - skipfirst;
  204. }
  205. return len;
  206. }
  207. /* Read inode INO from the mounted filesystem described by DATA. This
  208. inode is used by default now. */
  209. static grub_err_t
  210. grub_minix_read_inode (struct grub_minix_data *data, int ino)
  211. {
  212. struct grub_minix_sblock *sblock = &data->sblock;
  213. /* Block in which the inode is stored. */
  214. int block;
  215. data->ino = ino;
  216. /* The first inode in minix is inode 1. */
  217. ino--;
  218. block = ((2 + grub_le_to_cpu16 (sblock->inode_bmap_size)
  219. + grub_le_to_cpu16 (sblock->zone_bmap_size))
  220. << GRUB_MINIX_LOG2_BSIZE);
  221. if (data->version == 1)
  222. {
  223. block += ino / (GRUB_DISK_SECTOR_SIZE / sizeof (struct grub_minix_inode));
  224. int offs = (ino % (GRUB_DISK_SECTOR_SIZE
  225. / sizeof (struct grub_minix_inode))
  226. * sizeof (struct grub_minix_inode));
  227. grub_disk_read (data->disk, block, offs,
  228. sizeof (struct grub_minix_inode), &data->inode);
  229. }
  230. else
  231. {
  232. block += ino / (GRUB_DISK_SECTOR_SIZE
  233. / sizeof (struct grub_minix2_inode));
  234. int offs = (ino
  235. % (GRUB_DISK_SECTOR_SIZE / sizeof (struct grub_minix2_inode))
  236. * sizeof (struct grub_minix2_inode));
  237. grub_disk_read (data->disk, block, offs,
  238. sizeof (struct grub_minix2_inode),&data->inode2);
  239. }
  240. return GRUB_ERR_NONE;
  241. }
  242. /* Lookup the symlink the current inode points to. INO is the inode
  243. number of the directory the symlink is relative to. */
  244. static grub_err_t
  245. grub_minix_lookup_symlink (struct grub_minix_data *data, int ino)
  246. {
  247. char symlink[GRUB_MINIX_INODE_SIZE (data) + 1];
  248. if (++data->linknest > GRUB_MINIX_MAX_SYMLNK_CNT)
  249. return grub_error (GRUB_ERR_SYMLINK_LOOP, "too deep nesting of symlinks");
  250. if (grub_minix_read_file (data, 0, 0,
  251. GRUB_MINIX_INODE_SIZE (data), symlink) < 0)
  252. return grub_errno;
  253. symlink[GRUB_MINIX_INODE_SIZE (data)] = '\0';
  254. /* The symlink is an absolute path, go back to the root inode. */
  255. if (symlink[0] == '/')
  256. ino = GRUB_MINIX_ROOT_INODE;
  257. /* Now load in the old inode. */
  258. if (grub_minix_read_inode (data, ino))
  259. return grub_errno;
  260. grub_minix_find_file (data, symlink);
  261. if (grub_errno)
  262. grub_error (grub_errno, "cannot follow symlink `%s'", symlink);
  263. return grub_errno;
  264. }
  265. /* Find the file with the pathname PATH on the filesystem described by
  266. DATA. */
  267. static grub_err_t
  268. grub_minix_find_file (struct grub_minix_data *data, const char *path)
  269. {
  270. char fpath[grub_strlen (path) + 1];
  271. char *name = fpath;
  272. char *next;
  273. unsigned int pos = 0;
  274. int dirino;
  275. grub_strcpy (fpath, path);
  276. /* Skip the first slash. */
  277. if (name[0] == '/')
  278. {
  279. name++;
  280. if (!*name)
  281. return 0;
  282. }
  283. /* Extract the actual part from the pathname. */
  284. next = grub_strchr (name, '/');
  285. if (next)
  286. {
  287. next[0] = '\0';
  288. next++;
  289. }
  290. do
  291. {
  292. grub_uint16_t ino;
  293. char filename[data->filename_size + 1];
  294. if (grub_strlen (name) == 0)
  295. return GRUB_ERR_NONE;
  296. if (grub_minix_read_file (data, 0, pos, sizeof (ino),
  297. (char *) &ino) < 0)
  298. return grub_errno;
  299. if (grub_minix_read_file (data, 0, pos + sizeof (ino),
  300. data->filename_size, (char *) filename)< 0)
  301. return grub_errno;
  302. filename[data->filename_size] = '\0';
  303. /* Check if the current direntry matches the current part of the
  304. pathname. */
  305. if (!grub_strcmp (name, filename))
  306. {
  307. dirino = data->ino;
  308. grub_minix_read_inode (data, grub_le_to_cpu16 (ino));
  309. /* Follow the symlink. */
  310. if ((GRUB_MINIX_INODE_MODE (data)
  311. & GRUB_MINIX_IFLNK) == GRUB_MINIX_IFLNK)
  312. {
  313. grub_minix_lookup_symlink (data, dirino);
  314. if (grub_errno)
  315. return grub_errno;
  316. }
  317. if (!next)
  318. return 0;
  319. pos = 0;
  320. name = next;
  321. next = grub_strchr (name, '/');
  322. if (next)
  323. {
  324. next[0] = '\0';
  325. next++;
  326. }
  327. if ((GRUB_MINIX_INODE_MODE (data)
  328. & GRUB_MINIX_IFDIR) != GRUB_MINIX_IFDIR)
  329. return grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a directory");
  330. continue;
  331. }
  332. pos += sizeof (ino) + data->filename_size;
  333. } while (pos < GRUB_MINIX_INODE_SIZE (data));
  334. grub_error (GRUB_ERR_FILE_NOT_FOUND, "file not found");
  335. return grub_errno;
  336. }
  337. /* Mount the filesystem on the disk DISK. */
  338. static struct grub_minix_data *
  339. grub_minix_mount (grub_disk_t disk)
  340. {
  341. struct grub_minix_data *data;
  342. data = grub_malloc (sizeof (struct grub_minix_data));
  343. if (!data)
  344. return 0;
  345. /* Read the superblock. */
  346. grub_disk_read (disk, GRUB_MINIX_SBLOCK, 0,
  347. sizeof (struct grub_minix_sblock),&data->sblock);
  348. if (grub_errno)
  349. goto fail;
  350. if (grub_le_to_cpu16 (data->sblock.magic) == GRUB_MINIX_MAGIC)
  351. {
  352. data->version = 1;
  353. data->filename_size = 14;
  354. }
  355. else if (grub_le_to_cpu16 (data->sblock.magic) == GRUB_MINIX2_MAGIC)
  356. {
  357. data->version = 2;
  358. data->filename_size = 14;
  359. }
  360. else if (grub_le_to_cpu16 (data->sblock.magic) == GRUB_MINIX_MAGIC_30)
  361. {
  362. data->version = 1;
  363. data->filename_size = 30;
  364. }
  365. else if (grub_le_to_cpu16 (data->sblock.magic) == GRUB_MINIX2_MAGIC_30)
  366. {
  367. data->version = 2;
  368. data->filename_size = 30;
  369. }
  370. else
  371. goto fail;
  372. data->disk = disk;
  373. data->linknest = 0;
  374. return data;
  375. fail:
  376. grub_free (data);
  377. grub_error (GRUB_ERR_BAD_FS, "not a minix filesystem");
  378. return 0;
  379. }
  380. static grub_err_t
  381. grub_minix_dir (grub_device_t device, const char *path,
  382. int (*hook) (const char *filename,
  383. const struct grub_dirhook_info *info))
  384. {
  385. struct grub_minix_data *data = 0;
  386. struct grub_minix_sblock *sblock;
  387. unsigned int pos = 0;
  388. data = grub_minix_mount (device->disk);
  389. if (!data)
  390. return grub_errno;
  391. grub_minix_read_inode (data, GRUB_MINIX_ROOT_INODE);
  392. if (grub_errno)
  393. goto fail;
  394. sblock = &data->sblock;
  395. grub_minix_find_file (data, path);
  396. if (grub_errno)
  397. goto fail;
  398. if ((GRUB_MINIX_INODE_MODE (data) & GRUB_MINIX_IFDIR) != GRUB_MINIX_IFDIR)
  399. {
  400. grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a directory");
  401. goto fail;
  402. }
  403. while (pos < GRUB_MINIX_INODE_SIZE (data))
  404. {
  405. grub_uint16_t ino;
  406. char filename[data->filename_size + 1];
  407. int dirino = data->ino;
  408. struct grub_dirhook_info info;
  409. grub_memset (&info, 0, sizeof (info));
  410. if (grub_minix_read_file (data, 0, pos, sizeof (ino),
  411. (char *) &ino) < 0)
  412. return grub_errno;
  413. if (grub_minix_read_file (data, 0, pos + sizeof (ino),
  414. data->filename_size,
  415. (char *) filename) < 0)
  416. return grub_errno;
  417. filename[data->filename_size] = '\0';
  418. /* The filetype is not stored in the dirent. Read the inode to
  419. find out the filetype. This *REALLY* sucks. */
  420. grub_minix_read_inode (data, grub_le_to_cpu16 (ino));
  421. info.dir = ((GRUB_MINIX_INODE_MODE (data)
  422. & GRUB_MINIX_IFDIR) == GRUB_MINIX_IFDIR);
  423. if (hook (filename, &info) ? 1 : 0)
  424. break;
  425. /* Load the old inode back in. */
  426. grub_minix_read_inode (data, dirino);
  427. pos += sizeof (ino) + data->filename_size;
  428. }
  429. fail:
  430. grub_free (data);
  431. return grub_errno;
  432. }
  433. /* Open a file named NAME and initialize FILE. */
  434. static grub_err_t
  435. grub_minix_open (struct grub_file *file, const char *name)
  436. {
  437. struct grub_minix_data *data;
  438. data = grub_minix_mount (file->device->disk);
  439. if (!data)
  440. return grub_errno;
  441. /* Open the inode op the root directory. */
  442. grub_minix_read_inode (data, GRUB_MINIX_ROOT_INODE);
  443. if (grub_errno)
  444. {
  445. grub_free (data);
  446. return grub_errno;
  447. }
  448. if (!name || name[0] != '/')
  449. {
  450. grub_error (GRUB_ERR_BAD_FILENAME, "bad filename");
  451. return grub_errno;
  452. }
  453. /* Traverse the directory tree to the node that should be
  454. opened. */
  455. grub_minix_find_file (data, name);
  456. if (grub_errno)
  457. {
  458. grub_free (data);
  459. return grub_errno;
  460. }
  461. file->data = data;
  462. file->size = GRUB_MINIX_INODE_SIZE (data);
  463. return GRUB_ERR_NONE;
  464. }
  465. static grub_ssize_t
  466. grub_minix_read (grub_file_t file, char *buf, grub_size_t len)
  467. {
  468. struct grub_minix_data *data =
  469. (struct grub_minix_data *) file->data;
  470. return grub_minix_read_file (data, file->read_hook, file->offset, len, buf);
  471. }
  472. static grub_err_t
  473. grub_minix_close (grub_file_t file)
  474. {
  475. grub_free (file->data);
  476. return GRUB_ERR_NONE;
  477. }
  478. static grub_err_t
  479. grub_minix_label (grub_device_t device __attribute ((unused)),
  480. char **label __attribute ((unused)))
  481. {
  482. return GRUB_ERR_NONE;
  483. }
  484. static struct grub_fs grub_minix_fs =
  485. {
  486. .name = "minix",
  487. .dir = grub_minix_dir,
  488. .open = grub_minix_open,
  489. .read = grub_minix_read,
  490. .close = grub_minix_close,
  491. .label = grub_minix_label,
  492. .next = 0
  493. };
  494. GRUB_MOD_INIT(minix)
  495. {
  496. grub_fs_register (&grub_minix_fs);
  497. my_mod = mod;
  498. }
  499. GRUB_MOD_FINI(minix)
  500. {
  501. grub_fs_unregister (&grub_minix_fs);
  502. }