do_mounts_rd.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/kernel.h>
  3. #include <linux/fs.h>
  4. #include <linux/minix_fs.h>
  5. #include <linux/ext2_fs.h>
  6. #include <linux/romfs_fs.h>
  7. #include <uapi/linux/cramfs_fs.h>
  8. #include <linux/initrd.h>
  9. #include <linux/string.h>
  10. #include <linux/slab.h>
  11. #include "do_mounts.h"
  12. #include "../fs/squashfs/squashfs_fs.h"
  13. #include <linux/decompress/generic.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. * squashfs
  41. * gzip
  42. * bzip2
  43. * lzma
  44. * xz
  45. * lzo
  46. * lz4
  47. */
  48. static int __init
  49. identify_ramdisk_image(int fd, int start_block, decompress_fn *decompressor)
  50. {
  51. const int size = 512;
  52. struct minix_super_block *minixsb;
  53. struct romfs_super_block *romfsb;
  54. struct cramfs_super *cramfsb;
  55. struct squashfs_super_block *squashfsb;
  56. int nblocks = -1;
  57. unsigned char *buf;
  58. const char *compress_name;
  59. unsigned long n;
  60. buf = kmalloc(size, GFP_KERNEL);
  61. if (!buf)
  62. return -ENOMEM;
  63. minixsb = (struct minix_super_block *) buf;
  64. romfsb = (struct romfs_super_block *) buf;
  65. cramfsb = (struct cramfs_super *) buf;
  66. squashfsb = (struct squashfs_super_block *) buf;
  67. memset(buf, 0xe5, size);
  68. /*
  69. * Read block 0 to test for compressed kernel
  70. */
  71. ksys_lseek(fd, start_block * BLOCK_SIZE, 0);
  72. ksys_read(fd, buf, size);
  73. *decompressor = decompress_method(buf, size, &compress_name);
  74. if (compress_name) {
  75. printk(KERN_NOTICE "RAMDISK: %s image found at block %d\n",
  76. compress_name, start_block);
  77. if (!*decompressor)
  78. printk(KERN_EMERG
  79. "RAMDISK: %s decompressor not configured!\n",
  80. compress_name);
  81. nblocks = 0;
  82. goto done;
  83. }
  84. /* romfs is at block zero too */
  85. if (romfsb->word0 == ROMSB_WORD0 &&
  86. romfsb->word1 == ROMSB_WORD1) {
  87. printk(KERN_NOTICE
  88. "RAMDISK: romfs filesystem found at block %d\n",
  89. start_block);
  90. nblocks = (ntohl(romfsb->size)+BLOCK_SIZE-1)>>BLOCK_SIZE_BITS;
  91. goto done;
  92. }
  93. if (cramfsb->magic == CRAMFS_MAGIC) {
  94. printk(KERN_NOTICE
  95. "RAMDISK: cramfs filesystem found at block %d\n",
  96. start_block);
  97. nblocks = (cramfsb->size + BLOCK_SIZE - 1) >> BLOCK_SIZE_BITS;
  98. goto done;
  99. }
  100. /* squashfs is at block zero too */
  101. if (le32_to_cpu(squashfsb->s_magic) == SQUASHFS_MAGIC) {
  102. printk(KERN_NOTICE
  103. "RAMDISK: squashfs filesystem found at block %d\n",
  104. start_block);
  105. nblocks = (le64_to_cpu(squashfsb->bytes_used) + BLOCK_SIZE - 1)
  106. >> BLOCK_SIZE_BITS;
  107. goto done;
  108. }
  109. /*
  110. * Read 512 bytes further to check if cramfs is padded
  111. */
  112. ksys_lseek(fd, start_block * BLOCK_SIZE + 0x200, 0);
  113. ksys_read(fd, buf, size);
  114. if (cramfsb->magic == CRAMFS_MAGIC) {
  115. printk(KERN_NOTICE
  116. "RAMDISK: cramfs filesystem found at block %d\n",
  117. start_block);
  118. nblocks = (cramfsb->size + BLOCK_SIZE - 1) >> BLOCK_SIZE_BITS;
  119. goto done;
  120. }
  121. /*
  122. * Read block 1 to test for minix and ext2 superblock
  123. */
  124. ksys_lseek(fd, (start_block+1) * BLOCK_SIZE, 0);
  125. ksys_read(fd, buf, size);
  126. /* Try minix */
  127. if (minixsb->s_magic == MINIX_SUPER_MAGIC ||
  128. minixsb->s_magic == MINIX_SUPER_MAGIC2) {
  129. printk(KERN_NOTICE
  130. "RAMDISK: Minix filesystem found at block %d\n",
  131. start_block);
  132. nblocks = minixsb->s_nzones << minixsb->s_log_zone_size;
  133. goto done;
  134. }
  135. /* Try ext2 */
  136. n = ext2_image_size(buf);
  137. if (n) {
  138. printk(KERN_NOTICE
  139. "RAMDISK: ext2 filesystem found at block %d\n",
  140. start_block);
  141. nblocks = n;
  142. goto done;
  143. }
  144. printk(KERN_NOTICE
  145. "RAMDISK: Couldn't find valid RAM disk image starting at %d.\n",
  146. start_block);
  147. done:
  148. ksys_lseek(fd, start_block * BLOCK_SIZE, 0);
  149. kfree(buf);
  150. return nblocks;
  151. }
  152. int __init rd_load_image(char *from)
  153. {
  154. int res = 0;
  155. int in_fd, out_fd;
  156. unsigned long rd_blocks, devblocks;
  157. int nblocks, i, disk;
  158. char *buf = NULL;
  159. unsigned short rotate = 0;
  160. decompress_fn decompressor = NULL;
  161. #if !defined(CONFIG_S390)
  162. char rotator[4] = { '|' , '/' , '-' , '\\' };
  163. #endif
  164. out_fd = ksys_open("/dev/ram", O_RDWR, 0);
  165. if (out_fd < 0)
  166. goto out;
  167. in_fd = ksys_open(from, O_RDONLY, 0);
  168. if (in_fd < 0)
  169. goto noclose_input;
  170. nblocks = identify_ramdisk_image(in_fd, rd_image_start, &decompressor);
  171. if (nblocks < 0)
  172. goto done;
  173. if (nblocks == 0) {
  174. if (crd_load(in_fd, out_fd, decompressor) == 0)
  175. goto successful_load;
  176. goto done;
  177. }
  178. /*
  179. * NOTE NOTE: nblocks is not actually blocks but
  180. * the number of kibibytes of data to load into a ramdisk.
  181. */
  182. if (ksys_ioctl(out_fd, BLKGETSIZE, (unsigned long)&rd_blocks) < 0)
  183. rd_blocks = 0;
  184. else
  185. rd_blocks >>= 1;
  186. if (nblocks > rd_blocks) {
  187. printk("RAMDISK: image too big! (%dKiB/%ldKiB)\n",
  188. nblocks, rd_blocks);
  189. goto done;
  190. }
  191. /*
  192. * OK, time to copy in the data
  193. */
  194. if (ksys_ioctl(in_fd, BLKGETSIZE, (unsigned long)&devblocks) < 0)
  195. devblocks = 0;
  196. else
  197. devblocks >>= 1;
  198. if (strcmp(from, "/initrd.image") == 0)
  199. devblocks = nblocks;
  200. if (devblocks == 0) {
  201. printk(KERN_ERR "RAMDISK: could not determine device size\n");
  202. goto done;
  203. }
  204. buf = kmalloc(BLOCK_SIZE, GFP_KERNEL);
  205. if (!buf) {
  206. printk(KERN_ERR "RAMDISK: could not allocate buffer\n");
  207. goto done;
  208. }
  209. printk(KERN_NOTICE "RAMDISK: Loading %dKiB [%ld disk%s] into ram disk... ",
  210. nblocks, ((nblocks-1)/devblocks)+1, nblocks>devblocks ? "s" : "");
  211. for (i = 0, disk = 1; i < nblocks; i++) {
  212. if (i && (i % devblocks == 0)) {
  213. pr_cont("done disk #%d.\n", disk++);
  214. rotate = 0;
  215. if (ksys_close(in_fd)) {
  216. printk("Error closing the disk.\n");
  217. goto noclose_input;
  218. }
  219. change_floppy("disk #%d", disk);
  220. in_fd = ksys_open(from, O_RDONLY, 0);
  221. if (in_fd < 0) {
  222. printk("Error opening disk.\n");
  223. goto noclose_input;
  224. }
  225. printk("Loading disk #%d... ", disk);
  226. }
  227. ksys_read(in_fd, buf, BLOCK_SIZE);
  228. ksys_write(out_fd, buf, BLOCK_SIZE);
  229. #if !defined(CONFIG_S390)
  230. if (!(i % 16)) {
  231. pr_cont("%c\b", rotator[rotate & 0x3]);
  232. rotate++;
  233. }
  234. #endif
  235. }
  236. pr_cont("done.\n");
  237. successful_load:
  238. res = 1;
  239. done:
  240. ksys_close(in_fd);
  241. noclose_input:
  242. ksys_close(out_fd);
  243. out:
  244. kfree(buf);
  245. ksys_unlink("/dev/ram");
  246. return res;
  247. }
  248. int __init rd_load_disk(int n)
  249. {
  250. if (rd_prompt)
  251. change_floppy("root floppy disk to be loaded into RAM disk");
  252. create_dev("/dev/root", ROOT_DEV);
  253. create_dev("/dev/ram", MKDEV(RAMDISK_MAJOR, n));
  254. return rd_load_image("/dev/root");
  255. }
  256. static int exit_code;
  257. static int decompress_error;
  258. static int crd_infd, crd_outfd;
  259. static long __init compr_fill(void *buf, unsigned long len)
  260. {
  261. long r = ksys_read(crd_infd, buf, len);
  262. if (r < 0)
  263. printk(KERN_ERR "RAMDISK: error while reading compressed data");
  264. else if (r == 0)
  265. printk(KERN_ERR "RAMDISK: EOF while reading compressed data");
  266. return r;
  267. }
  268. static long __init compr_flush(void *window, unsigned long outcnt)
  269. {
  270. long written = ksys_write(crd_outfd, window, outcnt);
  271. if (written != outcnt) {
  272. if (decompress_error == 0)
  273. printk(KERN_ERR
  274. "RAMDISK: incomplete write (%ld != %ld)\n",
  275. written, outcnt);
  276. decompress_error = 1;
  277. return -1;
  278. }
  279. return outcnt;
  280. }
  281. static void __init error(char *x)
  282. {
  283. printk(KERN_ERR "%s\n", x);
  284. exit_code = 1;
  285. decompress_error = 1;
  286. }
  287. static int __init crd_load(int in_fd, int out_fd, decompress_fn deco)
  288. {
  289. int result;
  290. crd_infd = in_fd;
  291. crd_outfd = out_fd;
  292. if (!deco) {
  293. pr_emerg("Invalid ramdisk decompression routine. "
  294. "Select appropriate config option.\n");
  295. panic("Could not decompress initial ramdisk image.");
  296. }
  297. result = deco(NULL, 0, compr_fill, compr_flush, NULL, NULL, error);
  298. if (decompress_error)
  299. result = 1;
  300. return result;
  301. }