initrd.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. static 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 (area == NULL)
  34. return 0;
  35. if (load_initrd(initrd, area, size) == -1)
  36. return 0;
  37. initrd_start = (unsigned long) area;
  38. initrd_end = initrd_start + size;
  39. return 0;
  40. }
  41. __uml_postsetup(read_initrd);
  42. static int __init uml_initrd_setup(char *line, int *add)
  43. {
  44. initrd = line;
  45. return 0;
  46. }
  47. __uml_setup("initrd=", uml_initrd_setup,
  48. "initrd=<initrd image>\n"
  49. " This is used to boot UML from an initrd image. The argument is the\n"
  50. " name of the file containing the image.\n\n"
  51. );
  52. static int load_initrd(char *filename, void *buf, int size)
  53. {
  54. int fd, n;
  55. fd = os_open_file(filename, of_read(OPENFLAGS()), 0);
  56. if (fd < 0) {
  57. printk(KERN_ERR "Opening '%s' failed - err = %d\n", filename,
  58. -fd);
  59. return -1;
  60. }
  61. n = os_read_file(fd, buf, size);
  62. if (n != size) {
  63. printk(KERN_ERR "Read of %d bytes from '%s' failed, "
  64. "err = %d\n", size,
  65. filename, -n);
  66. return -1;
  67. }
  68. os_close_file(fd);
  69. return 0;
  70. }