initrd.c 1.7 KB

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