initrd.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL
  4. */
  5. #include <linux/init.h>
  6. #include <linux/bootmem.h>
  7. #include <linux/initrd.h>
  8. #include <asm/types.h>
  9. #include <init.h>
  10. #include <os.h>
  11. /* Changed by uml_initrd_setup, which is a setup */
  12. static char *initrd __initdata = NULL;
  13. static int load_initrd(char *filename, void *buf, int size);
  14. int __init read_initrd(void)
  15. {
  16. void *area;
  17. long long size;
  18. int err;
  19. if (initrd == NULL)
  20. return 0;
  21. err = os_file_size(initrd, &size);
  22. if (err)
  23. return 0;
  24. /*
  25. * This is necessary because alloc_bootmem craps out if you
  26. * ask for no memory.
  27. */
  28. if (size == 0) {
  29. printk(KERN_ERR "\"%s\" is a zero-size initrd\n", initrd);
  30. return 0;
  31. }
  32. area = alloc_bootmem(size);
  33. if (load_initrd(initrd, area, size) == -1)
  34. return 0;
  35. initrd_start = (unsigned long) area;
  36. initrd_end = initrd_start + size;
  37. return 0;
  38. }
  39. static int __init uml_initrd_setup(char *line, int *add)
  40. {
  41. initrd = line;
  42. return 0;
  43. }
  44. __uml_setup("initrd=", uml_initrd_setup,
  45. "initrd=<initrd image>\n"
  46. " This is used to boot UML from an initrd image. The argument is the\n"
  47. " name of the file containing the image.\n\n"
  48. );
  49. static int load_initrd(char *filename, void *buf, int size)
  50. {
  51. int fd, n;
  52. fd = os_open_file(filename, of_read(OPENFLAGS()), 0);
  53. if (fd < 0) {
  54. printk(KERN_ERR "Opening '%s' failed - err = %d\n", filename,
  55. -fd);
  56. return -1;
  57. }
  58. n = os_read_file(fd, buf, size);
  59. if (n != size) {
  60. printk(KERN_ERR "Read of %d bytes from '%s' failed, "
  61. "err = %d\n", size,
  62. filename, -n);
  63. return -1;
  64. }
  65. os_close_file(fd);
  66. return 0;
  67. }