do_mounts_rd.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. static struct file *in_file, *out_file;
  15. static loff_t in_pos, out_pos;
  16. static int __init prompt_ramdisk(char *str)
  17. {
  18. pr_warn("ignoring the deprecated prompt_ramdisk= option\n");
  19. return 1;
  20. }
  21. __setup("prompt_ramdisk=", prompt_ramdisk);
  22. int __initdata rd_image_start; /* starting block # of image */
  23. static int __init ramdisk_start_setup(char *str)
  24. {
  25. rd_image_start = simple_strtol(str,NULL,0);
  26. return 1;
  27. }
  28. __setup("ramdisk_start=", ramdisk_start_setup);
  29. static int __init crd_load(decompress_fn deco);
  30. /*
  31. * This routine tries to find a RAM disk image to load, and returns the
  32. * number of blocks to read for a non-compressed image, 0 if the image
  33. * is a compressed image, and -1 if an image with the right magic
  34. * numbers could not be found.
  35. *
  36. * We currently check for the following magic numbers:
  37. * minix
  38. * ext2
  39. * romfs
  40. * cramfs
  41. * squashfs
  42. * gzip
  43. * bzip2
  44. * lzma
  45. * xz
  46. * lzo
  47. * lz4
  48. */
  49. static int __init
  50. identify_ramdisk_image(struct file *file, loff_t pos,
  51. decompress_fn *decompressor)
  52. {
  53. const int size = 512;
  54. struct minix_super_block *minixsb;
  55. struct romfs_super_block *romfsb;
  56. struct cramfs_super *cramfsb;
  57. struct squashfs_super_block *squashfsb;
  58. int nblocks = -1;
  59. unsigned char *buf;
  60. const char *compress_name;
  61. unsigned long n;
  62. int start_block = rd_image_start;
  63. buf = kmalloc(size, GFP_KERNEL);
  64. if (!buf)
  65. return -ENOMEM;
  66. minixsb = (struct minix_super_block *) buf;
  67. romfsb = (struct romfs_super_block *) buf;
  68. cramfsb = (struct cramfs_super *) buf;
  69. squashfsb = (struct squashfs_super_block *) buf;
  70. memset(buf, 0xe5, size);
  71. /*
  72. * Read block 0 to test for compressed kernel
  73. */
  74. pos = start_block * BLOCK_SIZE;
  75. kernel_read(file, buf, size, &pos);
  76. *decompressor = decompress_method(buf, size, &compress_name);
  77. if (compress_name) {
  78. printk(KERN_NOTICE "RAMDISK: %s image found at block %d\n",
  79. compress_name, start_block);
  80. if (!*decompressor)
  81. printk(KERN_EMERG
  82. "RAMDISK: %s decompressor not configured!\n",
  83. compress_name);
  84. nblocks = 0;
  85. goto done;
  86. }
  87. /* romfs is at block zero too */
  88. if (romfsb->word0 == ROMSB_WORD0 &&
  89. romfsb->word1 == ROMSB_WORD1) {
  90. printk(KERN_NOTICE
  91. "RAMDISK: romfs filesystem found at block %d\n",
  92. start_block);
  93. nblocks = (ntohl(romfsb->size)+BLOCK_SIZE-1)>>BLOCK_SIZE_BITS;
  94. goto done;
  95. }
  96. if (cramfsb->magic == CRAMFS_MAGIC) {
  97. printk(KERN_NOTICE
  98. "RAMDISK: cramfs filesystem found at block %d\n",
  99. start_block);
  100. nblocks = (cramfsb->size + BLOCK_SIZE - 1) >> BLOCK_SIZE_BITS;
  101. goto done;
  102. }
  103. /* squashfs is at block zero too */
  104. if (le32_to_cpu(squashfsb->s_magic) == SQUASHFS_MAGIC) {
  105. printk(KERN_NOTICE
  106. "RAMDISK: squashfs filesystem found at block %d\n",
  107. start_block);
  108. nblocks = (le64_to_cpu(squashfsb->bytes_used) + BLOCK_SIZE - 1)
  109. >> BLOCK_SIZE_BITS;
  110. goto done;
  111. }
  112. /*
  113. * Read 512 bytes further to check if cramfs is padded
  114. */
  115. pos = start_block * BLOCK_SIZE + 0x200;
  116. kernel_read(file, buf, size, &pos);
  117. if (cramfsb->magic == CRAMFS_MAGIC) {
  118. printk(KERN_NOTICE
  119. "RAMDISK: cramfs filesystem found at block %d\n",
  120. start_block);
  121. nblocks = (cramfsb->size + BLOCK_SIZE - 1) >> BLOCK_SIZE_BITS;
  122. goto done;
  123. }
  124. /*
  125. * Read block 1 to test for minix and ext2 superblock
  126. */
  127. pos = (start_block + 1) * BLOCK_SIZE;
  128. kernel_read(file, buf, size, &pos);
  129. /* Try minix */
  130. if (minixsb->s_magic == MINIX_SUPER_MAGIC ||
  131. minixsb->s_magic == MINIX_SUPER_MAGIC2) {
  132. printk(KERN_NOTICE
  133. "RAMDISK: Minix filesystem found at block %d\n",
  134. start_block);
  135. nblocks = minixsb->s_nzones << minixsb->s_log_zone_size;
  136. goto done;
  137. }
  138. /* Try ext2 */
  139. n = ext2_image_size(buf);
  140. if (n) {
  141. printk(KERN_NOTICE
  142. "RAMDISK: ext2 filesystem found at block %d\n",
  143. start_block);
  144. nblocks = n;
  145. goto done;
  146. }
  147. printk(KERN_NOTICE
  148. "RAMDISK: Couldn't find valid RAM disk image starting at %d.\n",
  149. start_block);
  150. done:
  151. kfree(buf);
  152. return nblocks;
  153. }
  154. static unsigned long nr_blocks(struct file *file)
  155. {
  156. struct inode *inode = file->f_mapping->host;
  157. if (!S_ISBLK(inode->i_mode))
  158. return 0;
  159. return i_size_read(inode) >> 10;
  160. }
  161. int __init rd_load_image(char *from)
  162. {
  163. int res = 0;
  164. unsigned long rd_blocks, devblocks;
  165. int nblocks, i;
  166. char *buf = NULL;
  167. unsigned short rotate = 0;
  168. decompress_fn decompressor = NULL;
  169. #if !defined(CONFIG_S390)
  170. char rotator[4] = { '|' , '/' , '-' , '\\' };
  171. #endif
  172. out_file = filp_open("/dev/ram", O_RDWR, 0);
  173. if (IS_ERR(out_file))
  174. goto out;
  175. in_file = filp_open(from, O_RDONLY, 0);
  176. if (IS_ERR(in_file))
  177. goto noclose_input;
  178. in_pos = rd_image_start * BLOCK_SIZE;
  179. nblocks = identify_ramdisk_image(in_file, in_pos, &decompressor);
  180. if (nblocks < 0)
  181. goto done;
  182. if (nblocks == 0) {
  183. if (crd_load(decompressor) == 0)
  184. goto successful_load;
  185. goto done;
  186. }
  187. /*
  188. * NOTE NOTE: nblocks is not actually blocks but
  189. * the number of kibibytes of data to load into a ramdisk.
  190. */
  191. rd_blocks = nr_blocks(out_file);
  192. if (nblocks > rd_blocks) {
  193. printk("RAMDISK: image too big! (%dKiB/%ldKiB)\n",
  194. nblocks, rd_blocks);
  195. goto done;
  196. }
  197. /*
  198. * OK, time to copy in the data
  199. */
  200. if (strcmp(from, "/initrd.image") == 0)
  201. devblocks = nblocks;
  202. else
  203. devblocks = nr_blocks(in_file);
  204. if (devblocks == 0) {
  205. printk(KERN_ERR "RAMDISK: could not determine device size\n");
  206. goto done;
  207. }
  208. buf = kmalloc(BLOCK_SIZE, GFP_KERNEL);
  209. if (!buf) {
  210. printk(KERN_ERR "RAMDISK: could not allocate buffer\n");
  211. goto done;
  212. }
  213. printk(KERN_NOTICE "RAMDISK: Loading %dKiB [%ld disk%s] into ram disk... ",
  214. nblocks, ((nblocks-1)/devblocks)+1, nblocks>devblocks ? "s" : "");
  215. for (i = 0; i < nblocks; i++) {
  216. if (i && (i % devblocks == 0)) {
  217. pr_cont("done disk #1.\n");
  218. rotate = 0;
  219. fput(in_file);
  220. break;
  221. }
  222. kernel_read(in_file, buf, BLOCK_SIZE, &in_pos);
  223. kernel_write(out_file, buf, BLOCK_SIZE, &out_pos);
  224. #if !defined(CONFIG_S390)
  225. if (!(i % 16)) {
  226. pr_cont("%c\b", rotator[rotate & 0x3]);
  227. rotate++;
  228. }
  229. #endif
  230. }
  231. pr_cont("done.\n");
  232. successful_load:
  233. res = 1;
  234. done:
  235. fput(in_file);
  236. noclose_input:
  237. fput(out_file);
  238. out:
  239. kfree(buf);
  240. init_unlink("/dev/ram");
  241. return res;
  242. }
  243. int __init rd_load_disk(int n)
  244. {
  245. create_dev("/dev/root", ROOT_DEV);
  246. create_dev("/dev/ram", MKDEV(RAMDISK_MAJOR, n));
  247. return rd_load_image("/dev/root");
  248. }
  249. static int exit_code;
  250. static int decompress_error;
  251. static long __init compr_fill(void *buf, unsigned long len)
  252. {
  253. long r = kernel_read(in_file, buf, len, &in_pos);
  254. if (r < 0)
  255. printk(KERN_ERR "RAMDISK: error while reading compressed data");
  256. else if (r == 0)
  257. printk(KERN_ERR "RAMDISK: EOF while reading compressed data");
  258. return r;
  259. }
  260. static long __init compr_flush(void *window, unsigned long outcnt)
  261. {
  262. long written = kernel_write(out_file, window, outcnt, &out_pos);
  263. if (written != outcnt) {
  264. if (decompress_error == 0)
  265. printk(KERN_ERR
  266. "RAMDISK: incomplete write (%ld != %ld)\n",
  267. written, outcnt);
  268. decompress_error = 1;
  269. return -1;
  270. }
  271. return outcnt;
  272. }
  273. static void __init error(char *x)
  274. {
  275. printk(KERN_ERR "%s\n", x);
  276. exit_code = 1;
  277. decompress_error = 1;
  278. }
  279. static int __init crd_load(decompress_fn deco)
  280. {
  281. int result;
  282. if (!deco) {
  283. pr_emerg("Invalid ramdisk decompression routine. "
  284. "Select appropriate config option.\n");
  285. panic("Could not decompress initial ramdisk image.");
  286. }
  287. result = deco(NULL, 0, compr_fill, compr_flush, NULL, NULL, error);
  288. if (decompress_error)
  289. result = 1;
  290. return result;
  291. }