ofw_initrd.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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/cdefs.h>
  25. __FBSDID("$FreeBSD$");
  26. #include <sys/param.h>
  27. #include <sys/kernel.h>
  28. #include <sys/systm.h>
  29. #include <sys/module.h>
  30. #include <sys/types.h>
  31. #include <sys/proc.h>
  32. #include <vm/vm.h>
  33. #include <vm/pmap.h>
  34. #include <machine/bus.h>
  35. #include <machine/elf.h>
  36. #include <machine/param.h>
  37. #include <dev/ofw/openfirm.h>
  38. #include <dev/ofw/ofw_bus.h>
  39. #include <dev/ofw/ofw_bus_subr.h>
  40. #include "opt_md.h"
  41. extern u_char *mfs_root;
  42. extern int mfs_root_size;
  43. static void ofw_initrd_probe_and_attach(void *junk);
  44. SYSINIT(ofw_initrd_probe_and_attach, SI_SUB_KMEM, SI_ORDER_ANY,
  45. ofw_initrd_probe_and_attach, NULL);
  46. static void
  47. ofw_initrd_probe_and_attach(void *junk)
  48. {
  49. phandle_t chosen;
  50. vm_paddr_t start, end;
  51. pcell_t cell[2];
  52. ssize_t size;
  53. u_char *taste;
  54. Elf_Ehdr ehdr;
  55. if (!hw_direct_map)
  56. return;
  57. chosen = OF_finddevice("/chosen");
  58. if (chosen <= 0)
  59. return;
  60. if (!OF_hasprop(chosen, "linux,initrd-start") ||
  61. !OF_hasprop(chosen, "linux,initrd-end"))
  62. return;
  63. size = OF_getencprop(chosen, "linux,initrd-start", cell, sizeof(cell));
  64. if (size == 4)
  65. start = cell[0];
  66. else if (size == 8)
  67. start = (uint64_t)cell[0] << 32 | cell[1];
  68. else {
  69. printf("ofw_initrd: Wrong linux,initrd-start size\n");
  70. return;
  71. }
  72. size = OF_getencprop(chosen, "linux,initrd-end", cell, sizeof(cell));
  73. if (size == 4)
  74. end = cell[0];
  75. else if (size == 8)
  76. end = (uint64_t)cell[0] << 32 | cell[1];
  77. else{
  78. printf("ofw_initrd: Wrong linux,initrd-end size\n");
  79. return;
  80. }
  81. if (end - start > 0) {
  82. taste = (u_char*) PHYS_TO_DMAP(start);
  83. memcpy(&ehdr, taste, sizeof(ehdr));
  84. if (IS_ELF(ehdr)) {
  85. printf("ofw_initrd: initrd is kernel image!\n");
  86. return;
  87. }
  88. mfs_root = taste;
  89. mfs_root_size = end - start;
  90. printf("ofw_initrd: initrd loaded at 0x%08lx-0x%08lx\n",
  91. start, end);
  92. }
  93. }