do_mounts_rd.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. #include <linux/kernel.h>
  2. #include <linux/fs.h>
  3. #include <linux/minix_fs.h>
  4. #include <linux/ext2_fs.h>
  5. #include <linux/romfs_fs.h>
  6. #include <linux/cramfs_fs.h>
  7. #include <linux/initrd.h>
  8. #include <linux/string.h>
  9. #include "do_mounts.h"
  10. #include <linux/decompress/generic.h>
  11. #include <linux/decompress/bunzip2.h>
  12. #include <linux/decompress/unlzma.h>
  13. #include <linux/decompress/inflate.h>
  14. int __initdata rd_prompt = 1;/* 1 = prompt for RAM disk, 0 = don't prompt */
  15. static int __init prompt_ramdisk(char *str)
  16. {
  17. rd_prompt = simple_strtol(str,NULL,0) & 1;
  18. return 1;
  19. }
  20. __setup("prompt_ramdisk=", prompt_ramdisk);
  21. int __initdata rd_image_start; /* starting block # of image */
  22. static int __init ramdisk_start_setup(char *str)
  23. {
  24. rd_image_start = simple_strtol(str,NULL,0);
  25. return 1;
  26. }
  27. __setup("ramdisk_start=", ramdisk_start_setup);
  28. static int __init crd_load(int in_fd, int out_fd, decompress_fn deco);
  29. /*
  30. * This routine tries to find a RAM disk image to load, and returns the
  31. * number of blocks to read for a non-compressed image, 0 if the image
  32. * is a compressed image, and -1 if an image with the right magic
  33. * numbers could not be found.
  34. *
  35. * We currently check for the following magic numbers:
  36. * minix
  37. * ext2
  38. * romfs
  39. * cramfs
  40. * gzip
  41. */
  42. static const struct compress_format {
  43. unsigned char magic[2];
  44. const char *name;
  45. decompress_fn decompressor;
  46. } compressed_formats[] = {
  47. #ifdef CONFIG_RD_GZIP
  48. { {037, 0213}, "gzip", gunzip },
  49. { {037, 0236}, "gzip", gunzip },
  50. #endif
  51. #ifdef CONFIG_RD_BZIP2
  52. { {0x42, 0x5a}, "bzip2", bunzip2 },
  53. #endif
  54. #ifdef CONFIG_RD_LZMA
  55. { {0x5d, 0x00}, "lzma", unlzma },
  56. #endif
  57. { {0, 0}, NULL, NULL }
  58. };
  59. static int __init
  60. identify_ramdisk_image(int fd, int start_block, decompress_fn *decompressor)
  61. {
  62. const int size = 512;
  63. struct minix_super_block *minixsb;
  64. struct ext2_super_block *ext2sb;
  65. struct romfs_super_block *romfsb;
  66. struct cramfs_super *cramfsb;
  67. int nblocks = -1;
  68. unsigned char *buf;
  69. const struct compress_format *cf;
  70. buf = kmalloc(size, GFP_KERNEL);
  71. if (!buf)
  72. return -1;
  73. minixsb = (struct minix_super_block *) buf;
  74. ext2sb = (struct ext2_super_block *) buf;
  75. romfsb = (struct romfs_super_block *) buf;
  76. cramfsb = (struct cramfs_super *) buf;
  77. memset(buf, 0xe5, size);
  78. /*
  79. * Read block 0 to test for compressed kernel
  80. */
  81. sys_lseek(fd, start_block * BLOCK_SIZE, 0);
  82. sys_read(fd, buf, size);
  83. for (cf = compressed_formats; cf->decompressor; cf++) {
  84. if (buf[0] == cf->magic[0] && buf[1] == cf->magic[1]) {
  85. printk(KERN_NOTICE
  86. "RAMDISK: %s image found at block %d\n",
  87. cf->name, start_block);
  88. *decompressor = cf->decompressor;
  89. nblocks = 0;
  90. goto done;
  91. }
  92. }
  93. /* romfs is at block zero too */
  94. if (romfsb->word0 == ROMSB_WORD0 &&
  95. romfsb->word1 == ROMSB_WORD1) {
  96. printk(KERN_NOTICE
  97. "RAMDISK: romfs filesystem found at block %d\n",
  98. start_block);
  99. nblocks = (ntohl(romfsb->size)+BLOCK_SIZE-1)>>BLOCK_SIZE_BITS;
  100. goto done;
  101. }
  102. if (cramfsb->magic == CRAMFS_MAGIC) {
  103. printk(KERN_NOTICE
  104. "RAMDISK: cramfs filesystem found at block %d\n",
  105. start_block);
  106. nblocks = (cramfsb->size + BLOCK_SIZE - 1) >> BLOCK_SIZE_BITS;
  107. goto done;
  108. }
  109. /*
  110. * Read block 1 to test for minix and ext2 superblock
  111. */
  112. sys_lseek(fd, (start_block+1) * BLOCK_SIZE, 0);
  113. sys_read(fd, buf, size);
  114. /* Try minix */
  115. if (minixsb->s_magic == MINIX_SUPER_MAGIC ||
  116. minixsb->s_magic == MINIX_SUPER_MAGIC2) {
  117. printk(KERN_NOTICE
  118. "RAMDISK: Minix filesystem found at block %d\n",
  119. start_block);
  120. nblocks = minixsb->s_nzones << minixsb->s_log_zone_size;
  121. goto done;
  122. }
  123. /* Try ext2 */
  124. if (ext2sb->s_magic == cpu_to_le16(EXT2_SUPER_MAGIC)) {
  125. printk(KERN_NOTICE
  126. "RAMDISK: ext2 filesystem found at block %d\n",
  127. start_block);
  128. nblocks = le32_to_cpu(ext2sb->s_blocks_count) <<
  129. le32_to_cpu(ext2sb->s_log_block_size);
  130. goto done;
  131. }
  132. printk(KERN_NOTICE
  133. "RAMDISK: Couldn't find valid RAM disk image starting at %d.\n",
  134. start_block);
  135. done:
  136. sys_lseek(fd, start_block * BLOCK_SIZE, 0);
  137. kfree(buf);
  138. return nblocks;
  139. }
  140. int __init rd_load_image(char *from)
  141. {
  142. int res = 0;
  143. int in_fd, out_fd;
  144. unsigned long rd_blocks, devblocks;
  145. int nblocks, i, disk;
  146. char *buf = NULL;
  147. unsigned short rotate = 0;
  148. decompress_fn decompressor = NULL;
  149. #if !defined(CONFIG_S390) && !defined(CONFIG_PPC_ISERIES)
  150. char rotator[4] = { '|' , '/' , '-' , '\\' };
  151. #endif
  152. out_fd = sys_open("/dev/ram", O_RDWR, 0);
  153. if (out_fd < 0)
  154. goto out;
  155. in_fd = sys_open(from, O_RDONLY, 0);
  156. if (in_fd < 0)
  157. goto noclose_input;
  158. nblocks = identify_ramdisk_image(in_fd, rd_image_start, &decompressor);
  159. if (nblocks < 0)
  160. goto done;
  161. if (nblocks == 0) {
  162. if (crd_load(in_fd, out_fd, decompressor) == 0)
  163. goto successful_load;
  164. goto done;
  165. }
  166. /*
  167. * NOTE NOTE: nblocks is not actually blocks but
  168. * the number of kibibytes of data to load into a ramdisk.
  169. * So any ramdisk block size that is a multiple of 1KiB should
  170. * work when the appropriate ramdisk_blocksize is specified
  171. * on the command line.
  172. *
  173. * The default ramdisk_blocksize is 1KiB and it is generally
  174. * silly to use anything else, so make sure to use 1KiB
  175. * blocksize while generating ext2fs ramdisk-images.
  176. */
  177. if (sys_ioctl(out_fd, BLKGETSIZE, (unsigned long)&rd_blocks) < 0)
  178. rd_blocks = 0;
  179. else
  180. rd_blocks >>= 1;
  181. if (nblocks > rd_blocks) {
  182. printk("RAMDISK: image too big! (%dKiB/%ldKiB)\n",
  183. nblocks, rd_blocks);
  184. goto done;
  185. }
  186. /*
  187. * OK, time to copy in the data
  188. */
  189. if (sys_ioctl(in_fd, BLKGETSIZE, (unsigned long)&devblocks) < 0)
  190. devblocks = 0;
  191. else
  192. devblocks >>= 1;
  193. if (strcmp(from, "/initrd.image") == 0)
  194. devblocks = nblocks;
  195. if (devblocks == 0) {
  196. printk(KERN_ERR "RAMDISK: could not determine device size\n");
  197. goto done;
  198. }
  199. buf = kmalloc(BLOCK_SIZE, GFP_KERNEL);
  200. if (!buf) {
  201. printk(KERN_ERR "RAMDISK: could not allocate buffer\n");
  202. goto done;
  203. }
  204. printk(KERN_NOTICE "RAMDISK: Loading %dKiB [%ld disk%s] into ram disk... ",
  205. nblocks, ((nblocks-1)/devblocks)+1, nblocks>devblocks ? "s" : "");
  206. for (i = 0, disk = 1; i < nblocks; i++) {
  207. if (i && (i % devblocks == 0)) {
  208. printk("done disk #%d.\n", disk++);
  209. rotate = 0;
  210. if (sys_close(in_fd)) {
  211. printk("Error closing the disk.\n");
  212. goto noclose_input;
  213. }
  214. change_floppy("disk #%d", disk);
  215. in_fd = sys_open(from, O_RDONLY, 0);
  216. if (in_fd < 0) {
  217. printk("Error opening disk.\n");
  218. goto noclose_input;
  219. }
  220. printk("Loading disk #%d... ", disk);
  221. }
  222. sys_read(in_fd, buf, BLOCK_SIZE);
  223. sys_write(out_fd, buf, BLOCK_SIZE);
  224. #if !defined(CONFIG_S390) && !defined(CONFIG_PPC_ISERIES)
  225. if (!(i % 16)) {
  226. printk("%c\b", rotator[rotate & 0x3]);
  227. rotate++;
  228. }
  229. #endif
  230. }
  231. printk("done.\n");
  232. successful_load:
  233. res = 1;
  234. done:
  235. sys_close(in_fd);
  236. noclose_input:
  237. sys_close(out_fd);
  238. out:
  239. kfree(buf);
  240. sys_unlink("/dev/ram");
  241. return res;
  242. }
  243. int __init rd_load_disk(int n)
  244. {
  245. if (rd_prompt)
  246. change_floppy("root floppy disk to be loaded into RAM disk");
  247. create_dev("/dev/root", ROOT_DEV);
  248. create_dev("/dev/ram", MKDEV(RAMDISK_MAJOR, n));
  249. return rd_load_image("/dev/root");
  250. }
  251. static int exit_code;
  252. static int decompress_error;
  253. static int crd_infd, crd_outfd;
  254. static int __init compr_fill(void *buf, unsigned int len)
  255. {
  256. int r = sys_read(crd_infd, buf, len);
  257. if (r < 0)
  258. printk(KERN_ERR "RAMDISK: error while reading compressed data");
  259. else if (r == 0)
  260. printk(KERN_ERR "RAMDISK: EOF while reading compressed data");
  261. return r;
  262. }
  263. static int __init compr_flush(void *window, unsigned int outcnt)
  264. {
  265. int written = sys_write(crd_outfd, window, outcnt);
  266. if (written != outcnt) {
  267. if (decompress_error == 0)
  268. printk(KERN_ERR
  269. "RAMDISK: incomplete write (%d != %d)\n",
  270. written, outcnt);
  271. decompress_error = 1;
  272. return -1;
  273. }
  274. return outcnt;
  275. }
  276. static void __init error(char *x)
  277. {
  278. printk(KERN_ERR "%s\n", x);
  279. exit_code = 1;
  280. decompress_error = 1;
  281. }
  282. static int __init crd_load(int in_fd, int out_fd, decompress_fn deco)
  283. {
  284. int result;
  285. crd_infd = in_fd;
  286. crd_outfd = out_fd;
  287. result = deco(NULL, 0, compr_fill, compr_flush, NULL, NULL, error);
  288. if (decompress_error)
  289. result = 1;
  290. return result;
  291. }