do_mounts_rd.c 7.4 KB

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