78b11a830287f1bc419fb70b866edabf341d29f0
3 * Copyright (C) 2001 Brian Palmer <brianp@sginet.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "multiboot.h"
29 unsigned long next_module_load_base
= 0;
31 BOOL
MultiBootLoadKernel(FILE *KernelImage
)
33 DWORD ImageHeaders
[2048];
35 DWORD dwHeaderChecksum
;
36 DWORD dwFileLoadOffset
;
42 * Load the first 8192 bytes of the kernel image
43 * so we can search for the multiboot header
45 ReadFile(KernelImage
, 8192, NULL
, ImageHeaders
);
48 * Now find the multiboot header and copy it
50 for (Idx
=0; Idx
<2048; Idx
++)
53 if (ImageHeaders
[Idx
] == MULTIBOOT_HEADER_MAGIC
)
55 // Yes, copy it and break out of this loop
56 memcpy(&mb_header
, &ImageHeaders
[Idx
], sizeof(multiboot_header_t
));
63 * If we reached the end of the 8192 bytes without
64 * finding the multiboot header then return error
68 MessageBox("No multiboot header found!");
72 /*printf("multiboot header:\n");
73 printf("0x%x\n", mb_header.magic);
74 printf("0x%x\n", mb_header.flags);
75 printf("0x%x\n", mb_header.checksum);
76 printf("0x%x\n", mb_header.header_addr);
77 printf("0x%x\n", mb_header.load_addr);
78 printf("0x%x\n", mb_header.load_end_addr);
79 printf("0x%x\n", mb_header.bss_end_addr);
80 printf("0x%x\n", mb_header.entry_addr);
84 * Calculate the checksum and make sure it matches
86 dwHeaderChecksum
= mb_header
.magic
;
87 dwHeaderChecksum
+= mb_header
.flags
;
88 dwHeaderChecksum
+= mb_header
.checksum
;
89 if (dwHeaderChecksum
!= 0)
91 MessageBox("Multiboot header checksum invalid!");
96 * Get the file offset, this should be 0, and move the file pointer
98 dwFileLoadOffset
= (Idx
* sizeof(DWORD
)) - (mb_header
.header_addr
- mb_header
.load_addr
);
99 SetFilePointer(KernelImage
, dwFileLoadOffset
);
102 * Load the file image
104 dwDataSize
= (mb_header
.load_end_addr
- mb_header
.load_addr
);
105 ReadFile(KernelImage
, dwDataSize
, NULL
, (void*)mb_header
.load_addr
);
108 * Initialize bss area
110 dwBssSize
= (mb_header
.bss_end_addr
- mb_header
.load_end_addr
);
111 memset((void*)mb_header
.load_end_addr
, 0, dwBssSize
);
113 next_module_load_base
= ROUND_UP(mb_header
.bss_end_addr
, /*PAGE_SIZE*/4096);
118 BOOL
MultiBootLoadModule(FILE *ModuleImage
, char *ModuleName
)
122 char* ModuleNameString
;
126 * Get current module data structure and module name string array
128 pModule
= &multiboot_modules
[mb_info
.mods_count
];
130 TempName
= strchr( ModuleName
, '\\' );
132 ModuleName
= TempName
+ 1;
135 ModuleNameString
= multiboot_module_strings
[mb_info
.mods_count
];
137 dwModuleSize
= GetFileSize(ModuleImage
);
138 pModule
->mod_start
= next_module_load_base
;
139 pModule
->mod_end
= next_module_load_base
+ dwModuleSize
;
140 strcpy(ModuleNameString
, ModuleName
);
141 pModule
->string
= (unsigned long)ModuleNameString
;
144 * Load the file image
146 ReadFile(ModuleImage
, dwModuleSize
, NULL
, (void*)next_module_load_base
);
148 next_module_load_base
= ROUND_UP(pModule
->mod_end
, /*PAGE_SIZE*/4096);
149 mb_info
.mods_count
++;
154 int GetBootPartition(char *OperatingSystemName
)
156 int BootPartitionNumber
= -1;
160 if (OpenSection(OperatingSystemName
, &SectionId
))
162 if (ReadSectionSettingByName(SectionId
, "BootPartition", value
, 1024))
164 BootPartitionNumber
= atoi(value
);
168 return BootPartitionNumber
;