bunch of work in progress on getting x86_64 bootstrap working.
[newos.git] / boot / x86_64 / stage1.c
blobfa37a752035b497f0fb64493f0adef1635e49be3
1 /*
2 ** Copyright 2001-2004, Travis Geiselbrecht. All rights reserved.
3 ** Distributed under the terms of the NewOS License.
4 */
5 #include <string.h>
6 #include <stdarg.h>
7 #include <stdio.h>
8 #include <boot/stage2.h>
9 #include <boot/bootdir.h>
10 #include "stage1.h"
11 #include "inflate.h"
13 // needed for console stuff
14 static unsigned short *kScreenBase = (unsigned short*) 0xb8000;
15 static unsigned screenOffset = 0;
16 static unsigned int line = 0;
18 #define SCREEN_WIDTH 80
19 #define SCREEN_HEIGHT 25
20 #define PAGE_SIZE 4096
22 static unsigned char *heap_ptr = (unsigned char *)0x1000000;
24 extern void *_end;
25 #define TARGET ((void *)0x400000)
27 void _start(unsigned int mem, void *ext_mem_block, int ext_mem_count, int in_vesa, unsigned int vesa_ptr)
29 unsigned long len;
30 boot_dir *bootdir = TARGET;
31 void (*stage2_entry)(unsigned int mem, void *ext_mem_block, int ext_mem_count, int in_vesa, unsigned int vesa_ptr, int console_ptr);
33 clearscreen();
35 dprintf("stage1 boot, decompressing system");
37 len = gunzip((unsigned char const *)&_end, TARGET, kmalloc(32*1024));
38 dprintf("done, len %d\n", len);
40 dprintf("finding stage2...");
41 stage2_entry = (void*)((char *)bootdir + bootdir->bd_entry[1].be_offset * PAGE_SIZE + bootdir->bd_entry[1].be_code_ventr);
42 dprintf("entry at %p\n", stage2_entry);
44 // jump into stage2
45 stage2_entry(mem, ext_mem_block, ext_mem_count, in_vesa, vesa_ptr, screenOffset);
48 void *kmalloc(unsigned int size)
50 // dprintf("kmalloc: size %d, ptr %p\n", size, heap_ptr - size);
52 return (heap_ptr -= size);
55 void kfree(void *ptr)
59 void clearscreen()
61 int i;
63 for(i=0; i< SCREEN_WIDTH*SCREEN_HEIGHT*2; i++) {
64 kScreenBase[i] = 0xf20;
68 static void scrup()
70 int i;
71 memcpy(kScreenBase, kScreenBase + SCREEN_WIDTH,
72 SCREEN_WIDTH * SCREEN_HEIGHT * 2 - SCREEN_WIDTH * 2);
73 screenOffset = (SCREEN_HEIGHT - 1) * SCREEN_WIDTH;
74 for(i=0; i<SCREEN_WIDTH; i++)
75 kScreenBase[screenOffset + i] = 0x0720;
76 line = SCREEN_HEIGHT - 1;
79 void puts(const char *str)
81 while (*str) {
82 if (*str == '\n') {
83 line++;
84 if(line > SCREEN_HEIGHT - 1)
85 scrup();
86 else
87 screenOffset += SCREEN_WIDTH - (screenOffset % 80);
88 } else {
89 kScreenBase[screenOffset++] = 0xf00 | *str;
91 if (screenOffset >= SCREEN_WIDTH * SCREEN_HEIGHT)
92 scrup();
94 str++;
98 int dprintf(const char *fmt, ...)
100 int ret;
101 va_list args;
102 char temp[256];
104 va_start(args, fmt);
105 ret = vsprintf(temp,fmt,args);
106 va_end(args);
108 puts(temp);
109 return ret;
113 int panic(const char *fmt, ...)
115 int ret;
116 va_list args;
117 char temp[256];
119 va_start(args, fmt);
120 ret = vsprintf(temp,fmt,args);
121 va_end(args);
123 puts("STAGE1 PANIC: ");
124 puts(temp);
125 puts("\n");
127 puts("spinning forever...");
128 for(;;);
129 return ret;