ofw_initrd.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*-
  2. * Copyright (C) 2018 Breno Leitao
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  14. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  15. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  16. * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  17. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  18. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  19. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  20. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  21. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  22. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #include <sys/param.h>
  25. #include <sys/kernel.h>
  26. #include <sys/systm.h>
  27. #include <sys/module.h>
  28. #include <sys/types.h>
  29. #include <sys/proc.h>
  30. #include <vm/vm.h>
  31. #include <vm/pmap.h>
  32. #include <machine/bus.h>
  33. #include <machine/elf.h>
  34. #include <machine/param.h>
  35. #include <dev/ofw/openfirm.h>
  36. #include <dev/ofw/ofw_bus.h>
  37. #include <dev/ofw/ofw_bus_subr.h>
  38. #include "opt_md.h"
  39. extern u_char *mfs_root;
  40. extern int mfs_root_size;
  41. static void ofw_initrd_probe_and_attach(void *junk);
  42. SYSINIT(ofw_initrd_probe_and_attach, SI_SUB_KMEM, SI_ORDER_ANY,
  43. ofw_initrd_probe_and_attach, NULL);
  44. static void
  45. ofw_initrd_probe_and_attach(void *junk)
  46. {
  47. phandle_t chosen;
  48. vm_paddr_t start, end;
  49. pcell_t cell[2];
  50. ssize_t size;
  51. u_char *taste;
  52. Elf_Ehdr ehdr;
  53. if (!hw_direct_map)
  54. return;
  55. chosen = OF_finddevice("/chosen");
  56. if (chosen <= 0)
  57. return;
  58. if (!OF_hasprop(chosen, "linux,initrd-start") ||
  59. !OF_hasprop(chosen, "linux,initrd-end"))
  60. return;
  61. size = OF_getencprop(chosen, "linux,initrd-start", cell, sizeof(cell));
  62. if (size == 4)
  63. start = cell[0];
  64. else if (size == 8)
  65. start = (uint64_t)cell[0] << 32 | cell[1];
  66. else {
  67. printf("ofw_initrd: Wrong linux,initrd-start size\n");
  68. return;
  69. }
  70. size = OF_getencprop(chosen, "linux,initrd-end", cell, sizeof(cell));
  71. if (size == 4)
  72. end = cell[0];
  73. else if (size == 8)
  74. end = (uint64_t)cell[0] << 32 | cell[1];
  75. else{
  76. printf("ofw_initrd: Wrong linux,initrd-end size\n");
  77. return;
  78. }
  79. if (end - start > 0) {
  80. taste = (u_char*) PHYS_TO_DMAP(start);
  81. memcpy(&ehdr, taste, sizeof(ehdr));
  82. if (IS_ELF(ehdr)) {
  83. printf("ofw_initrd: initrd is kernel image!\n");
  84. return;
  85. }
  86. mfs_root = taste;
  87. mfs_root_size = end - start;
  88. printf("ofw_initrd: initrd loaded at 0x%08lx-0x%08lx\n",
  89. start, end);
  90. }
  91. }