boot_module.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-07-15 JasonHu,GUI first version
  9. */
  10. #include "boot_module.h"
  11. #include "multiboot2.h"
  12. #include <string.h>
  13. static void boot_module_init(struct multiboot_tag *tag)
  14. {
  15. struct boot_modules_info_block *modules_info = (struct boot_modules_info_block *)BOOT_MODULE_INFO_ADDR;
  16. int index = modules_info->modules_num;
  17. if (index >= MAX_BOOT_MODULES_NUM
  18. || modules_info->modules_size + ((struct multiboot_tag_module *)tag)->size > MAX_BOOT_MODULES_SIZE)
  19. {
  20. return;
  21. }
  22. modules_info->modules[index].size = ((struct multiboot_tag_module *)tag)->size;
  23. modules_info->modules[index].start = ((struct multiboot_tag_module *)tag)->mod_start;
  24. modules_info->modules[index].end = ((struct multiboot_tag_module *)tag)->mod_end;
  25. modules_info->modules[index].type = BOOT_MODULE_UNKNOWN;
  26. modules_info->modules_size += modules_info->modules[index].size;
  27. ++modules_info->modules_num;
  28. }
  29. static void boot_memory_init(struct multiboot_tag *tag)
  30. {
  31. unsigned long mem_upper = ((struct multiboot_tag_basic_meminfo *)tag)->mem_upper;
  32. unsigned long mem_lower = ((struct multiboot_tag_basic_meminfo *)tag)->mem_lower;
  33. // memory size store in 0x000001000
  34. *((unsigned int *)0x000001000) = ((mem_upper - mem_lower) << 10) + 0x100000;
  35. }
  36. int rt_boot_setup_entry(unsigned long magic, unsigned long addr)
  37. {
  38. // whether a multiboot
  39. if (magic != MULTIBOOT2_BOOTLOADER_MAGIC || addr & 7)
  40. return -1;
  41. struct multiboot_tag *tag;
  42. boot_module_info_init();
  43. for (tag = (struct multiboot_tag*)(addr + 8); tag->type != MULTIBOOT_TAG_TYPE_END; \
  44. tag = (struct multiboot_tag*)((rt_uint8_t *)tag + ((tag->size + 7) & ~7)))
  45. {
  46. switch (tag->type)
  47. {
  48. case MULTIBOOT_TAG_TYPE_MODULE:
  49. boot_module_init(tag);
  50. break;
  51. case MULTIBOOT_TAG_TYPE_BASIC_MEMINFO:
  52. boot_memory_init(tag);
  53. break;
  54. default: // other type, do nothing
  55. break;
  56. }
  57. }
  58. return 0;
  59. }