Skip to content
This repository was archived by the owner on Jan 14, 2026. It is now read-only.

Commit 6c3e4de

Browse files
committed
fmt
1 parent f4d483a commit 6c3e4de

4 files changed

Lines changed: 95 additions & 52 deletions

File tree

src/kernel/mem/vmem.c

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ uint64_t vmem_virt_to_phys64(uint64_t virt) {
9595
return virt;
9696
}
9797
if (current_mode == VMEM_MODE_OFFSET) {
98-
if ((int64_t)virt - vmem_offset < 0) return 0;
98+
if ((int64_t)virt - vmem_offset < 0)
99+
return 0;
99100
uint64_t phys = (uint64_t)((int64_t)virt - vmem_offset);
100101
return phys;
101102
}
@@ -107,8 +108,8 @@ uint64_t vmem_virt_to_phys64(uint64_t virt) {
107108
/* indices */
108109
uint64_t pml4_idx = (virt >> 39) & 0x1FFULL;
109110
uint64_t pdpt_idx = (virt >> 30) & 0x1FFULL;
110-
uint64_t pd_idx = (virt >> 21) & 0x1FFULL;
111-
uint64_t pt_idx = (virt >> 12) & 0x1FFULL;
111+
uint64_t pd_idx = (virt >> 21) & 0x1FFULL;
112+
uint64_t pt_idx = (virt >> 12) & 0x1FFULL;
112113
uint64_t page_off = virt & 0xFFFULL;
113114

114115
uint64_t read_entry;
@@ -117,19 +118,23 @@ uint64_t vmem_virt_to_phys64(uint64_t virt) {
117118
/* read PML4E */
118119
uint64_t pml4e_phys = pml4_base + (pml4_idx * 8);
119120
uint64_t pml4e_virt = vmem_phys_to_virt64(pml4e_phys);
120-
if (pml4e_virt == UINT64_MAX) return 0;
121+
if (pml4e_virt == UINT64_MAX)
122+
return 0;
121123
read_entry = *((volatile uint64_t *)(uintptr_t)pml4e_virt);
122124
entry = read_entry;
123-
if ((entry & 0x1) == 0) return 0; /* not present */
125+
if ((entry & 0x1) == 0)
126+
return 0; /* not present */
124127

125128
/* PDPT */
126129
uint64_t pdpt_base = entry & 0xFFFFFFFFFFFFF000ULL;
127130
uint64_t pdpte_phys = pdpt_base + (pdpt_idx * 8);
128131
uint64_t pdpte_virt = vmem_phys_to_virt64(pdpte_phys);
129-
if (pdpte_virt == UINT64_MAX) return 0;
132+
if (pdpte_virt == UINT64_MAX)
133+
return 0;
130134
read_entry = *((volatile uint64_t *)(uintptr_t)pdpte_virt);
131135
entry = read_entry;
132-
if ((entry & 0x1) == 0) return 0;
136+
if ((entry & 0x1) == 0)
137+
return 0;
133138
/* 1 GiB page? (PS bit) */
134139
if (entry & (1ULL << 7)) {
135140
uint64_t base = entry & 0xFFFFFC0000000ULL; /* bits 51:30 */
@@ -141,10 +146,12 @@ uint64_t vmem_virt_to_phys64(uint64_t virt) {
141146
uint64_t pd_base = entry & 0xFFFFFFFFFFFFF000ULL;
142147
uint64_t pde_phys = pd_base + (pd_idx * 8);
143148
uint64_t pde_virt = vmem_phys_to_virt64(pde_phys);
144-
if (pde_virt == UINT64_MAX) return 0;
149+
if (pde_virt == UINT64_MAX)
150+
return 0;
145151
read_entry = *((volatile uint64_t *)(uintptr_t)pde_virt);
146152
entry = read_entry;
147-
if ((entry & 0x1) == 0) return 0;
153+
if ((entry & 0x1) == 0)
154+
return 0;
148155
/* 2 MiB page? (PS bit) */
149156
if (entry & (1ULL << 7)) {
150157
uint64_t base = entry & 0xFFFFFFFFFFE00000ULL; /* bits 51:21 */
@@ -156,10 +163,12 @@ uint64_t vmem_virt_to_phys64(uint64_t virt) {
156163
uint64_t pt_base = entry & 0xFFFFFFFFFFFFF000ULL;
157164
uint64_t pte_phys = pt_base + (pt_idx * 8);
158165
uint64_t pte_virt = vmem_phys_to_virt64(pte_phys);
159-
if (pte_virt == UINT64_MAX) return 0;
166+
if (pte_virt == UINT64_MAX)
167+
return 0;
160168
read_entry = *((volatile uint64_t *)(uintptr_t)pte_virt);
161169
entry = read_entry;
162-
if ((entry & 0x1) == 0) return 0;
170+
if ((entry & 0x1) == 0)
171+
return 0;
163172

164173
uint64_t page_base = entry & 0xFFFFFFFFFFFFF000ULL;
165174
return page_base + page_off;

src/kernel/util/bdf.c

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ static int parse_bdf(const char *data, size_t size) {
135135
printk("BDF: Too many glyphs\n");
136136
return 0;
137137
}
138-
138+
139139
current_glyph = &font.glyphs[font.num_glyphs];
140140
current_glyph->width = font.width;
141141
current_glyph->height = font.height;
@@ -144,9 +144,9 @@ static int parse_bdf(const char *data, size_t size) {
144144
current_glyph->encoding = 0;
145145
bitmap_line = 0;
146146

147-
for (int zi = 0; zi < MAX_GLYPH_HEIGHT; zi++) {
148-
current_glyph->bitmap[zi] = 0;
149-
}
147+
for (int zi = 0; zi < MAX_GLYPH_HEIGHT; zi++) {
148+
current_glyph->bitmap[zi] = 0;
149+
}
150150

151151
in_bitmap = 0;
152152
} else if (starts_with(p, "ENCODING")) {
@@ -187,27 +187,37 @@ static int parse_bdf(const char *data, size_t size) {
187187
in_bitmap = 0;
188188
} else if (in_bitmap && current_glyph) {
189189
p = skip_spaces(p);
190-
if ((*p >= '0' && *p <= '9') || (*p >= 'A' && *p <= 'F') ||
191-
(*p >= 'a' && *p <= 'f')) {
190+
if ((*p >= '0' && *p <= '9') ||
191+
(*p >= 'A' && *p <= 'F') ||
192+
(*p >= 'a' && *p <= 'f')) {
192193
if (bitmap_line < MAX_GLYPH_HEIGHT) {
193194
char hex_str[9];
194195
int i = 0;
195196
while (i < (int)(sizeof(hex_str) - 1) &&
196-
((*p >= '0' && *p <= '9') || (*p >= 'A' && *p <= 'F') ||
197-
(*p >= 'a' && *p <= 'f'))) {
197+
((*p >= '0' && *p <= '9') ||
198+
(*p >= 'A' && *p <= 'F') ||
199+
(*p >= 'a' && *p <= 'f'))) {
198200
hex_str[i++] = *p++;
199201
}
200202
hex_str[i] = '\0';
201203
uint32_t val = hex_to_int(hex_str);
202204
int hex_len = i;
203205
int total_bits = hex_len * 4;
204-
int width = current_glyph->width ? current_glyph->width : font.width;
205-
if (width <= 0) width = font.width;
206+
int width =
207+
current_glyph->width ?
208+
current_glyph->width :
209+
font.width;
210+
if (width <= 0)
211+
width = font.width;
206212
if (total_bits > width) {
207213
val >>= (total_bits - width);
208214
}
209-
uint32_t mask = (width >= 32) ? 0xFFFFFFFFu : ((1u << width) - 1u);
210-
current_glyph->bitmap[bitmap_line++] = (uint16_t)(val & mask);
215+
uint32_t mask =
216+
(width >= 32) ?
217+
0xFFFFFFFFu :
218+
((1u << width) - 1u);
219+
current_glyph->bitmap[bitmap_line++] =
220+
(uint16_t)(val & mask);
211221
}
212222
}
213223
}

src/kernel/util/console.c

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,34 @@ static uint32_t fb_fg_color = 0xFFFFFF; // 白
2424
static uint32_t fb_bg_color = 0x000000; // 黒
2525

2626
static void allocate_gfx_buf_if_needed(void) {
27-
if (!use_framebuffer) return;
28-
if (gfx_buf) return;
27+
if (!use_framebuffer)
28+
return;
29+
if (gfx_buf)
30+
return;
2931

3032
const bdf_font_t *font = bdf_get_font();
3133
int fw = font ? (int)font->width : 8;
3234
int fh = font ? (int)font->height : 16;
33-
if (fw <= 0) fw = 8;
34-
if (fh <= 0) fh = 16;
35+
if (fw <= 0)
36+
fw = 8;
37+
if (fh <= 0)
38+
fh = 16;
3539

3640
gfx_cols = (int)(fb_width / fw);
3741
gfx_rows = (int)(fb_height / fh);
38-
if (gfx_cols <= 0) gfx_cols = 1;
39-
if (gfx_rows <= 0) gfx_rows = 1;
42+
if (gfx_cols <= 0)
43+
gfx_cols = 1;
44+
if (gfx_rows <= 0)
45+
gfx_rows = 1;
4046

4147
uint32_t size = (uint32_t)(gfx_cols * gfx_rows);
4248
gfx_buf = (char *)kmalloc(size);
4349
if (!gfx_buf) {
4450
gfx_cols = gfx_rows = 0;
4551
return;
4652
}
47-
for (uint32_t i = 0; i < size; i++) gfx_buf[i] = ' ';
53+
for (uint32_t i = 0; i < size; i++)
54+
gfx_buf[i] = ' ';
4855
}
4956

5057
/**
@@ -101,24 +108,28 @@ static void draw_char_fb(int x, int y, char c) {
101108

102109
const bdf_glyph_t *glyph = bdf_get_glyph((uint32_t)(unsigned char)c);
103110

104-
int char_width = (glyph && glyph->width) ? (int)glyph->width : (int)font_info->width;
105-
int char_height = (glyph && glyph->height) ? (int)glyph->height : (int)font_info->height;
111+
int char_width = (glyph && glyph->width) ? (int)glyph->width :
112+
(int)font_info->width;
113+
int char_height = (glyph && glyph->height) ? (int)glyph->height :
114+
(int)font_info->height;
106115

107116
if (!glyph || c == ' ') {
108117
for (int row = 0; row < char_height; row++) {
109118
for (int col = 0; col < char_width; col++) {
110119
uint32_t pixel_x = x * char_width + col;
111120
uint32_t pixel_y = y * char_height + row;
112121
if (pixel_x < fb_width && pixel_y < fb_height) {
113-
uint32_t offset = pixel_y * fb_pitch + pixel_x;
122+
uint32_t offset =
123+
pixel_y * fb_pitch + pixel_x;
114124
framebuffer[offset] = fb_bg_color;
115125
}
116126
}
117127
}
118128
return;
119129
}
120130

121-
for (int row = 0; row < char_height && row < (int)MAX_GLYPH_HEIGHT; row++) {
131+
for (int row = 0; row < char_height && row < (int)MAX_GLYPH_HEIGHT;
132+
row++) {
122133
uint16_t bits = glyph->bitmap[row];
123134
for (int col = 0; col < char_width; col++) {
124135
uint32_t pixel_x = x * char_width + col;
@@ -127,8 +138,12 @@ static void draw_char_fb(int x, int y, char c) {
127138
if (pixel_x < fb_width && pixel_y < fb_height) {
128139
uint32_t offset = pixel_y * fb_pitch + pixel_x;
129140
int bitpos = (char_width - 1) - col;
130-
uint16_t mask = (bitpos >= 0 && bitpos < 16) ? (1u << bitpos) : 0;
131-
framebuffer[offset] = (bits & mask) ? fb_fg_color : fb_bg_color;
141+
uint16_t mask = (bitpos >= 0 && bitpos < 16) ?
142+
(1u << bitpos) :
143+
0;
144+
framebuffer[offset] = (bits & mask) ?
145+
fb_fg_color :
146+
fb_bg_color;
132147
}
133148
}
134149
}
@@ -256,11 +271,13 @@ void new_line() {
256271
for (int r = 0; r < gfx_rows - 1; r++) {
257272
char *dst = gfx_buf + r * line_size;
258273
char *src = gfx_buf + (r + 1) * line_size;
259-
for (int c = 0; c < line_size; c++) dst[c] = src[c];
274+
for (int c = 0; c < line_size; c++)
275+
dst[c] = src[c];
260276
}
261277
/* clear last line */
262278
char *last = gfx_buf + (gfx_rows - 1) * line_size;
263-
for (int c = 0; c < line_size; c++) last[c] = ' ';
279+
for (int c = 0; c < line_size; c++)
280+
last[c] = ' ';
264281
cursor_row = gfx_rows - 1;
265282
console_render_text_to_fb();
266283
}
@@ -281,7 +298,8 @@ void new_line() {
281298
} else {
282299
for (int i = 0; i < N_HISTORY - 1; ++i) {
283300
for (int c = 0; c < CONSOLE_COLS; ++c)
284-
history[i][c] = history[i + 1][c];
301+
history[i][c] =
302+
history[i + 1][c];
285303
}
286304
for (int c = 0; c < CONSOLE_COLS; ++c)
287305
history[N_HISTORY - 1][c] = linebuf[c];
@@ -290,7 +308,8 @@ void new_line() {
290308
for (int r = 0; r < CONSOLE_ROWS - 1; r++) {
291309
for (int c = 0; c < CONSOLE_COLS; c++) {
292310
int dst = (r * CONSOLE_COLS + c) * 2;
293-
int src = ((r + 1) * CONSOLE_COLS + c) * 2;
311+
int src = ((r + 1) * CONSOLE_COLS + c) *
312+
2;
294313
video[dst] = video[src];
295314
video[dst + 1] = video[src + 1];
296315
}
@@ -303,8 +322,8 @@ void new_line() {
303322
}
304323
cursor_row = CONSOLE_ROWS - 1;
305324
history_offset = (history_lines > CONSOLE_ROWS) ?
306-
history_lines - CONSOLE_ROWS :
307-
0;
325+
history_lines - CONSOLE_ROWS :
326+
0;
308327
if (use_framebuffer) {
309328
console_render_text_to_fb();
310329
}
@@ -430,9 +449,11 @@ void console_scroll_page_down(void) {
430449
}
431450

432451
void console_render_text_to_fb(void) {
433-
if (!use_framebuffer) return;
452+
if (!use_framebuffer)
453+
return;
434454
const bdf_font_t *font_info = bdf_get_font();
435-
if (!font_info) return;
455+
if (!font_info)
456+
return;
436457
/* If we have an allocated gfx buffer (sized to fb/font), render from it
437458
* so we can draw beyond 80x25. Otherwise fall back to video memory. */
438459
if (gfx_buf && gfx_cols > 0 && gfx_rows > 0) {
@@ -448,7 +469,8 @@ void console_render_text_to_fb(void) {
448469

449470
int fb_cols = (int)(fb_width / font_info->width);
450471
int fb_rows = (int)(fb_height / font_info->height);
451-
if (fb_cols <= 0 || fb_rows <= 0) return;
472+
if (fb_cols <= 0 || fb_rows <= 0)
473+
return;
452474

453475
int cols = (fb_cols < CONSOLE_COLS) ? fb_cols : CONSOLE_COLS;
454476
int rows = (fb_rows < CONSOLE_ROWS) ? fb_rows : CONSOLE_ROWS;
@@ -472,8 +494,10 @@ void console_post_font_init(void) {
472494
if (gfx_buf && gfx_cols > 0 && gfx_rows > 0) {
473495
/* copy existing video memory (80x25) into gfx_buf top-left */
474496
uint8_t *video = (uint8_t *)VIDEO_MEMORY;
475-
int copy_rows = (gfx_rows < CONSOLE_ROWS) ? gfx_rows : CONSOLE_ROWS;
476-
int copy_cols = (gfx_cols < CONSOLE_COLS) ? gfx_cols : CONSOLE_COLS;
497+
int copy_rows = (gfx_rows < CONSOLE_ROWS) ? gfx_rows :
498+
CONSOLE_ROWS;
499+
int copy_cols = (gfx_cols < CONSOLE_COLS) ? gfx_cols :
500+
CONSOLE_COLS;
477501
for (int r = 0; r < copy_rows; ++r) {
478502
for (int c = 0; c < copy_cols; ++c) {
479503
int vpos = (r * CONSOLE_COLS + c) * 2;
@@ -487,7 +511,8 @@ void console_post_font_init(void) {
487511
}
488512
/* if gfx_rows > CONSOLE_ROWS, clear remaining rows */
489513
for (int r = copy_rows; r < gfx_rows; ++r) {
490-
for (int c = 0; c < gfx_cols; ++c) gfx_buf[r * gfx_cols + c] = ' ';
514+
for (int c = 0; c < gfx_cols; ++c)
515+
gfx_buf[r * gfx_cols + c] = ' ';
491516
}
492517

493518
/* Render to framebuffer */

src/kernel/util/init_msg.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,9 @@ void kernel_init() {
124124
block_cache_destroy(cache);
125125
}
126126

127-
/* initialize font and then allow console to allocate gfx buffer */
128-
init_font();
129-
console_post_font_init();
130-
127+
/* initialize font and then allow console to allocate gfx buffer */
128+
init_font();
129+
console_post_font_init();
131130
}
132131
}
133132
#ifdef INIT_MSG

0 commit comments

Comments
 (0)