initrd.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.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 "kern_util.h"
  10. #include "initrd.h"
  11. #include "init.h"
  12. #include "os.h"
  13. /* Changed by uml_initrd_setup, which is a setup */
  14. static char *initrd __initdata = NULL;
  15. static int __init read_initrd(void)
  16. {
  17. void *area;
  18. long long size;
  19. int err;
  20. if(initrd == NULL)
  21. return 0;
  22. err = os_file_size(initrd, &size);
  23. if(err)
  24. return 0;
  25. area = alloc_bootmem(size);
  26. if(area == NULL)
  27. return 0;
  28. if(load_initrd(initrd, area, size) == -1)
  29. return 0;
  30. initrd_start = (unsigned long) area;
  31. initrd_end = initrd_start + size;
  32. return 0;
  33. }
  34. __uml_postsetup(read_initrd);
  35. static int __init uml_initrd_setup(char *line, int *add)
  36. {
  37. initrd = line;
  38. return 0;
  39. }
  40. __uml_setup("initrd=", uml_initrd_setup,
  41. "initrd=<initrd image>\n"
  42. " This is used to boot UML from an initrd image. The argument is the\n"
  43. " name of the file containing the image.\n\n"
  44. );
  45. int load_initrd(char *filename, void *buf, int size)
  46. {
  47. int fd, n;
  48. fd = os_open_file(filename, of_read(OPENFLAGS()), 0);
  49. if(fd < 0){
  50. printk("Opening '%s' failed - err = %d\n", filename, -fd);
  51. return -1;
  52. }
  53. n = os_read_file(fd, buf, size);
  54. if(n != size){
  55. printk("Read of %d bytes from '%s' failed, err = %d\n", size,
  56. filename, -n);
  57. return -1;
  58. }
  59. os_close_file(fd);
  60. return 0;
  61. }