1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
/*
Copyright 2019 Benji Dial
Permission to use, copy, modify, and/or distribute this
software for any purpose with or without fee is hereby
granted, provided that the above copyright notice and this
permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS
ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
OF THIS SOFTWARE.
*/
#include "vga.h"
#include "files.h"
#include "mem.h"
#include "proc.h"
#include <stdbool.h>
enum tag_type {
BOOT_COMMAND = 1,
LOADER_NAME = 2,
BOOT_MODULES = 3,
MEMORY_INFO = 4,
BOOT_DEVICE = 5,
MEMORY_MAP = 6,
VBE_INFO = 7,
FBUF_INFO = 8,
ELF_SYMBOLS = 9,
APM_TABLE = 10,
EFI_I386_TABLE = 11,
EFI_AMD64_TABLE = 12,
SMBIOS_TABLE = 13,
RSDP_ACPI1 = 14,
RSDP_ACPI2 = 15,
NETWORK_INFO = 16,
EFI_MEMORY_MAP = 17,
EFI_SERVICES = 18,
EFI_I386_HANDLE = 19,
EFI_AMD64_HANDLE = 20,
IMAGE_BASE_ADDR = 21
};
struct tag_start {
uint32_t type;
uint32_t size;
} __attribute__ ((__packed__));
struct boot_device_tag {
uint32_t bios_device;
uint32_t partition;
uint32_t subpartition;
} __attribute__ ((__packed__));
enum mem_type {
AVAILABLE = 1,
HARDWARET = 2,
ACPI = 3,
PRESERVE = 4,
DEFECTIVE = 5
};
struct mmap_tag_entry {
uint64_t base;
uint64_t length;
uint32_t type;
} __attribute__ ((__packed__));
struct boot_device_tag boot_device;
bool have_boot_device = false;
bool have_mmap = false;
enum error_codes {
NO_BOOT_DEVICE = 0x00000000,
NO_MMAP = 0x00000001,
INSUFF_MEMORY = 0x00000002,
MMAP_TOO_SMALL = 0x00000003
};
struct tag_start *tag_pointer;
uint32_t main(void) {
clear();
/*
uint32_t *debug_ptr = (uint32_t *)tag_pointer - 2;
uint32_t debug_size = *debug_ptr / 4;
for (uint32_t i = 0; i < debug_size; ++i) {
put_32_hex(debug_ptr[i]);
put_char(' ');
}
*/
put_sz("Multiboot info:\n");
while (tag_pointer->type) {
put_sz(" Tag type 0x");
put_32_hex(tag_pointer->type);
put_sz(" with size 0x");
put_32_hex(tag_pointer->size);
switch (tag_pointer->type) {
case BOOT_DEVICE:
boot_device = *(struct boot_device_tag *)(tag_pointer + 1);
have_boot_device = true;
put_sz(": boot device\n BIOS code: ");
put_32_hex(boot_device.bios_device);
if (boot_device.partition != 0xffffffff) {
put_sz("\n Partition number: ");
put_32_hex(boot_device.partition);
if (boot_device.subpartition != 0xffffffff) {
put_sz("\n Subpartition number: ");
put_32_hex(boot_device.subpartition);
put_char('\n');
}
else
put_sz("\n No subpartition\n");
}
else
put_sz("\n No partition\n");
break;
case MEMORY_MAP:
{
uint32_t size = *(uint32_t *)(tag_pointer + 1);
struct mmap_tag_entry *tag = (struct mmap_tag_entry *)(tag_pointer + 2);
struct mmap_tag_entry *end = (struct mmap_tag_entry *)((uint32_t)tag_pointer + tag_pointer->size);
struct mmap_entry *entry = mmap_bss;
uint32_t bitmask = 0x00000001;
uint32_t byte = 0;
struct mmap_entry **last_next = &mmap_start;
uint32_t usable = 0;
put_sz(": memory map\n Regions:\n");
while (tag != end) {
if (!(tag->base & 0xffffffff00000000)) {
entry->base = (uint32_t)tag->base;
entry->length = (tag->base + tag->length - 1) & 0xffffffff00000000 ?
1 + ~(uint32_t)tag->base : (uint32_t)tag->length;
put_sz(" 0x");
put_32_hex(entry->base);
put_sz(" - 0x");
put_32_hex(entry->base + entry->length - 1);
switch (tag->type) {
case AVAILABLE:
put_sz(": usable\n");
entry->whose = FREE;
usable += entry->length;
break;
case HARDWARET:
case PRESERVE:
put_sz(": hardware use\n");
entry->whose = HARDWARE;
break;
case ACPI:
put_sz(": ACPI info\n");
entry->whose = HARDWARE;
break;
case DEFECTIVE:
put_sz(": defective\n");
entry->whose = HARDWARE;
break;
default:
put_sz(": assuming hardware use (0x");
put_32_hex(tag->type);
put_sz(")\n");
entry->whose = HARDWARE;
break;
}
*last_next = entry;
last_next = &entry->next;
mmap_bitmap[byte] |= bitmask;
++entry;
if (!(bitmask <<= 1)) {
bitmask = 0x00000001;
if (++byte == MMAP_SIZE)
return MMAP_TOO_SMALL;
}
}
tag = (struct mmap_tag_entry *)((uint32_t)tag + size);
}
put_sz(" Total usable memory: 0x");
put_32_hex(usable);
put_char('\n');
bool dirty;
do {
dirty = false;
for (struct mmap_entry *current = mmap_start; current; current = current->next)
for (struct mmap_entry *against = mmap_start; against; against = against->next)
if ((current->base + current->length == against->base) &&
(current->whose == against->whose)) {
current->length += against->length;
unmark_entry(against);
if (against == mmap_start)
mmap_start = against->next;
else {
for (current = mmap_start; current->next != against; current = current->next)
;
current->next = against->next;
}
dirty = true;
}
} while (dirty);
have_mmap = true;
break;
}
default:
put_sz(": ignoring\n");
}
tag_pointer = (struct tag_start *)(((uint32_t)tag_pointer + tag_pointer->size - 1 & 0xfffffff8) + 8);
}
if (!have_boot_device)
return NO_BOOT_DEVICE;
if (!have_mmap)
return NO_MMAP;
put_sz("\nReady!\n Memory map size: 0x");
put_32_hex(sizeof(struct mmap_entry) * MMAP_SIZE);
put_sz("\n Process table size: 0x");
put_32_hex(sizeof(struct proc_info) * 65536);
put_sz("\n File table size: 0x");
put_32_hex(sizeof(struct file_info) * 65536);
put_char('\n');
put_sz("\nWelcome to Portland version 0.0.9!\n");
while (1)
asm ("hlt");
}
void wrapped_main(void) {
uint32_t error = main();
set_color(0x47);
clear();
put_sz("Error: 0x");
put_32_hex(error);
}
|