of.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) Paul Mackerras 1997.
  4. */
  5. #include <stdarg.h>
  6. #include <stddef.h>
  7. #include "types.h"
  8. #include "elf.h"
  9. #include "string.h"
  10. #include "stdio.h"
  11. #include "page.h"
  12. #include "ops.h"
  13. #include "of.h"
  14. /* Value picked to match that used by yaboot */
  15. #define PROG_START 0x01400000 /* only used on 64-bit systems */
  16. #define RAM_END (512<<20) /* Fixme: use OF */
  17. #define ONE_MB 0x100000
  18. static unsigned long claim_base;
  19. void epapr_platform_init(unsigned long r3, unsigned long r4, unsigned long r5,
  20. unsigned long r6, unsigned long r7);
  21. static void *of_try_claim(unsigned long size)
  22. {
  23. unsigned long addr = 0;
  24. if (claim_base == 0)
  25. claim_base = _ALIGN_UP((unsigned long)_end, ONE_MB);
  26. for(; claim_base < RAM_END; claim_base += ONE_MB) {
  27. #ifdef DEBUG
  28. printf(" trying: 0x%08lx\n\r", claim_base);
  29. #endif
  30. addr = (unsigned long) of_claim(claim_base, size, 0);
  31. if (addr != PROM_ERROR)
  32. break;
  33. }
  34. if (addr == 0)
  35. return NULL;
  36. claim_base = PAGE_ALIGN(claim_base + size);
  37. return (void *)addr;
  38. }
  39. static void of_image_hdr(const void *hdr)
  40. {
  41. const Elf64_Ehdr *elf64 = hdr;
  42. if (elf64->e_ident[EI_CLASS] == ELFCLASS64) {
  43. /*
  44. * Maintain a "magic" minimum address. This keeps some older
  45. * firmware platforms running.
  46. */
  47. if (claim_base < PROG_START)
  48. claim_base = PROG_START;
  49. }
  50. }
  51. static void of_platform_init(unsigned long a1, unsigned long a2, void *promptr)
  52. {
  53. platform_ops.image_hdr = of_image_hdr;
  54. platform_ops.malloc = of_try_claim;
  55. platform_ops.exit = of_exit;
  56. platform_ops.vmlinux_alloc = of_vmlinux_alloc;
  57. dt_ops.finddevice = of_finddevice;
  58. dt_ops.getprop = of_getprop;
  59. dt_ops.setprop = of_setprop;
  60. of_console_init();
  61. of_init(promptr);
  62. loader_info.promptr = promptr;
  63. if (a1 && a2 && a2 != 0xdeadbeef) {
  64. loader_info.initrd_addr = a1;
  65. loader_info.initrd_size = a2;
  66. }
  67. }
  68. void platform_init(unsigned long r3, unsigned long r4, unsigned long r5,
  69. unsigned long r6, unsigned long r7)
  70. {
  71. /* Detect OF vs. ePAPR boot */
  72. if (r5)
  73. of_platform_init(r3, r4, (void *)r5);
  74. else
  75. epapr_platform_init(r3, r4, r5, r6, r7);
  76. }