1616// limitations under the License.
1717
1818#include < os>
19+ #include < kprint>
1920#include < boot/multiboot.h>
2021
2122#define MYINFO (X,...) INFO(" Kernel" , X, ##__VA_ARGS__)
2223
24+ extern " C" {
25+
26+ extern uintptr_t _end;
27+
28+ // Function to deterimine the end of multiboot provided data
29+ // (e.g. multiboot's data area as offset to the _end symbol)
30+ uintptr_t _multiboot_free_begin (uintptr_t boot_addr){
31+
32+ multiboot_info_t * bootinfo = (multiboot_info_t *) boot_addr;
33+ uintptr_t multi_end = reinterpret_cast <uintptr_t >(&_end);
34+
35+ if (bootinfo->flags & MULTIBOOT_INFO_CMDLINE
36+ and bootinfo->cmdline > multi_end) {
37+ debug (" * Multiboot cmdline end: 0x%x \n " , bootinfo->cmdline );
38+ multi_end = bootinfo->cmdline ;
39+ }
40+
41+ debug (" * Multiboot end: 0x%x \n " , multi_end);
42+
43+
44+ if (not bootinfo->mods_count )
45+ return multi_end;
46+
47+ multiboot_module_t * mods_list = (multiboot_module_t *)bootinfo->mods_addr ;
48+ debug (" * Module list @ %p \n " ,mods_list);
49+
50+ for (multiboot_module_t * mod = mods_list;
51+ mod < mods_list + bootinfo->mods_count ;
52+ mod ++) {
53+
54+ debug (" \t * Module @ %p \n " , (void *)mod->mod_start );
55+ debug (" \t * Args: %s \n " , (char *)mod->cmdline );
56+ debug (" \t * End: %p \n " , (char *)mod->mod_end );
57+
58+ if (mod->mod_end > multi_end)
59+ multi_end = mod->mod_end ;
60+
61+ }
62+
63+ debug (" * Multiboot end: 0x%x \n " , multi_end);
64+ return multi_end;
65+ }
66+ }
67+
2368void OS::multiboot (uint32_t boot_magic, uint32_t boot_addr){
2469 MYINFO (" Booted with multiboot" );
25- INFO2 (" * magic value: 0x%x Multiboot info at 0x%x" , boot_magic, boot_addr);
2670
27- multiboot_info_t * bootinfo = (multiboot_info_t *) boot_addr;
71+ INFO2 (" * magic value: 0x%x Multiboot info at 0x%x" , boot_magic, boot_addr);
72+ bootinfo_ = (multiboot_info_t *) boot_addr;
2873
29- if (! bootinfo ->flags & MULTIBOOT_INFO_MEMORY) {
74+ if (! bootinfo_ ->flags & MULTIBOOT_INFO_MEMORY) {
3075 INFO2 (" * No memory info provided in multiboot info" );
3176 return ;
3277 }
3378
3479 uint32_t mem_low_start = 0 ;
35- uint32_t mem_low_end = (bootinfo ->mem_lower * 1024 ) - 1 ;
36- uint32_t mem_low_kb = bootinfo ->mem_lower ;
80+ uint32_t mem_low_end = (bootinfo_ ->mem_lower * 1024 ) - 1 ;
81+ uint32_t mem_low_kb = bootinfo_ ->mem_lower ;
3782 uint32_t mem_high_start = 0x100000 ;
38- uint32_t mem_high_end = mem_high_start + (bootinfo ->mem_upper * 1024 ) - 1 ;
39- uint32_t mem_high_kb = bootinfo ->mem_upper ;
83+ uint32_t mem_high_end = mem_high_start + (bootinfo_ ->mem_upper * 1024 ) - 1 ;
84+ uint32_t mem_high_kb = bootinfo_ ->mem_upper ;
4085
4186 OS::low_memory_size_ = mem_low_kb * 1024 ;
4287 OS::high_memory_size_ = mem_high_kb * 1024 ;
@@ -49,26 +94,39 @@ void OS::multiboot(uint32_t boot_magic, uint32_t boot_addr){
4994 mem_high_start, mem_high_end, mem_high_kb);
5095 INFO2 (" " );
5196
52- if (bootinfo->flags & MULTIBOOT_INFO_CMDLINE) {
53- INFO2 (" * Booted with parameters @ %p: %s" ,(void *)bootinfo->cmdline , (char *)bootinfo->cmdline );
54- cmdline = std::string ((char *) bootinfo->cmdline );
97+ if (bootinfo_->flags & MULTIBOOT_INFO_CMDLINE) {
98+ INFO2 (" * Booted with parameters @ 0x%x: %s" , bootinfo_->cmdline ,
99+ reinterpret_cast <const char *>(bootinfo_->cmdline ));
100+ cmdline = reinterpret_cast <const char *>(bootinfo_->cmdline );
55101 }
56102
57- if (bootinfo ->flags & MULTIBOOT_INFO_MEM_MAP) {
103+ if (bootinfo_ ->flags & MULTIBOOT_INFO_MEM_MAP) {
58104 INFO2 (" * Multiboot provided memory map (%i entries @ %p)" ,
59- bootinfo ->mmap_length / sizeof (multiboot_memory_map_t ), (void *)bootinfo ->mmap_addr );
60- gsl::span<multiboot_memory_map_t > mmap { reinterpret_cast <multiboot_memory_map_t *>(bootinfo ->mmap_addr ),
61- (int )(bootinfo ->mmap_length / sizeof (multiboot_memory_map_t ))};
105+ bootinfo_ ->mmap_length / sizeof (multiboot_memory_map_t ), (void *)bootinfo_ ->mmap_addr );
106+ gsl::span<multiboot_memory_map_t > mmap { reinterpret_cast <multiboot_memory_map_t *>(bootinfo_ ->mmap_addr ),
107+ (int )(bootinfo_ ->mmap_length / sizeof (multiboot_memory_map_t ))};
62108
63109 for (auto map : mmap) {
64110 const char * str_type = map.type & MULTIBOOT_MEMORY_AVAILABLE ? " FREE" : " RESERVED" ;
65111 INFO2 (" \t 0x%08llx - 0x%08llx %s (%llu Kb.)" ,
66112 map.addr , map.addr + map.len - 1 , str_type, map.len / 1024 );
67113
68114 if (not (map.type & MULTIBOOT_MEMORY_AVAILABLE)) {
69- memory_map ().assign_range ({static_cast <uintptr_t >(map.addr ), static_cast <uintptr_t >(map.addr + map.len - 1 ), " Reserved" , " Multiboot / BIOS" });
115+ memory_map ().assign_range ({static_cast <uintptr_t >(map.addr ),
116+ static_cast <uintptr_t >(map.addr + map.len - 1 ), " Reserved" , " Multiboot / BIOS" });
70117 }
71118 }
72119 printf (" \n " );
73120 }
121+
122+ Span_mods mods = modules ();
123+
124+ if (not mods.empty ()) {
125+ MYINFO (" OS loaded with %i modules" , mods.size ());
126+ for (auto mod : mods) {
127+ INFO2 (" * %s @ 0x%x - 0x%x, size: %ib" ,
128+ reinterpret_cast <char *>(mod.cmdline ),
129+ mod.mod_start , mod.mod_end , mod.mod_end - mod.mod_start );
130+ }
131+ }
74132}
0 commit comments