Skip to content

Commit 143b564

Browse files
committedFeb 12, 2024·
buffer overflow fixes
1 parent f1f8bb2 commit 143b564

File tree

8 files changed

+60
-38
lines changed

8 files changed

+60
-38
lines changed
 

‎CMakeLists.txt

+5-4
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${OUTPUT_DIR}")
2121
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${OUTPUT_DIR}")
2222
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${OUTPUT_DIR}")
2323

24-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ffunction-sections -fdata-sections -O2")
25-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ffunction-sections -fdata-sections -O2")
24+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ffast-math -feliminate-unused-debug-types -ffunction-sections -fdata-sections -O2")
25+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ffast-math -feliminate-unused-debug-types -ffunction-sections -fdata-sections -O2")
2626

2727
if (PICO_PLATFORM STREQUAL "host")
2828
set(SDL2_LIB_DIR C:/SDL/lib)
@@ -130,12 +130,13 @@ else ()
130130
hardware_clocks
131131
hardware_pwm
132132
hardware_flash
133+
134+
tinyusb_board
135+
tinyusb_device
133136
)
134137
target_link_options(${PROJECT_NAME} PRIVATE "LINKER:--script=${CMAKE_CURRENT_LIST_DIR}/memmap.ld")
135138
set_target_properties(${PROJECT_NAME} PROPERTIES LINK_DEPENDS ${CMAKE_CURRENT_LIST_DIR}/memmap.ld)
136139

137-
family_configure_device_example(${PROJECT_NAME} noos)
138-
139140
target_sources(${PROJECT_NAME} PUBLIC
140141
${CMAKE_CURRENT_SOURCE_DIR}/drivers/usb/usb.c
141142
${CMAKE_CURRENT_SOURCE_DIR}/drivers/usb/msc_disk.c

‎drivers/usb/msc_disk.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ void tud_msc_capacity_cb(uint8_t lun, uint32_t* block_count, uint16_t* block_siz
161161
if (dio == RES_OK) {
162162
*block_count = dw;
163163
} else {
164-
char tmp[80]; sprintf(tmp, "disk_ioctl(GET_SECTOR_COUNT) failed: %d", dio); logMsg(tmp);
164+
// char tmp[80]; sprintf(tmp, "disk_ioctl(GET_SECTOR_COUNT) failed: %d", dio); logMsg(tmp);
165165
*block_count = 0;
166166
return;
167167
}
@@ -175,7 +175,7 @@ void tud_msc_capacity_cb(uint8_t lun, uint32_t* block_count, uint16_t* block_siz
175175
// - Start = 0 : stopped power mode, if load_eject = 1 : unload disk storage
176176
// - Start = 1 : active mode, if load_eject = 1 : load disk storage
177177
bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, bool load_eject) {
178-
char tmp[81]; sprintf(tmp, "power_condition: 0x%X start: %d load_eject: %d", power_condition, start, load_eject); logMsg(tmp);
178+
// char tmp[81]; sprintf(tmp, "power_condition: 0x%X start: %d load_eject: %d", power_condition, start, load_eject); logMsg(tmp);
179179
(void) lun;
180180
(void) power_condition;
181181
if ( load_eject ) {

‎drivers/vga-nextgen/vga.c

+36-18
Original file line numberDiff line numberDiff line change
@@ -502,26 +502,43 @@ void clrScr(uint8_t color) {
502502
current_line = start_debug_line;
503503
};
504504

505-
void draw_text(char* string, int x, int y, uint8_t color, uint8_t bgcolor) {
506-
/*if ((y < 0) | (y >= text_buffer_height)) return;
507-
if (x >= text_buffer_width) return;
508-
int len = strlen(string);
509-
if (x < 0) {
510-
if ((len + x) > 0) {
511-
string += -x;
512-
x = 0;
513-
}
514-
else {
515-
return;
516-
}
517-
}*/
518-
uint8_t* t_buf = text_buffer + text_buffer_width * 2 * y + 2 * x;
519-
for (int xi = x; xi < text_buffer_width * 2; xi++) {
520-
if (!(*string)) break;
505+
void draw_text(const char string[TEXTMODE_COLS + 1], uint32_t x, uint32_t y, uint8_t color, uint8_t bgcolor) {
506+
uint8_t* t_buf = text_buffer + TEXTMODE_COLS * 2 * y + 2 * x;
507+
for (int xi = TEXTMODE_COLS * 2; xi--;) {
508+
if (!*string) break;
521509
*t_buf++ = *string++;
522-
*t_buf++ = (bgcolor << 4) | (color & 0xF);
510+
*t_buf++ = bgcolor << 4 | color & 0xF;
523511
}
524-
};
512+
}
513+
514+
void draw_window(const char title[TEXTMODE_COLS + 1], uint32_t x, uint32_t y, uint32_t width, uint32_t height) {
515+
char line[width + 1];
516+
memset(line, 0, sizeof line);
517+
width--;
518+
height--;
519+
// Рисуем рамки
520+
521+
memset(line, 0xCD, width); // ═══
522+
523+
524+
line[0] = 0xC9; // ╔
525+
line[width] = 0xBB; // ╗
526+
draw_text(line, x, y, 11, 1);
527+
528+
line[0] = 0xC8; // ╚
529+
line[width] = 0xBC; // ╝
530+
draw_text(line, x, height + y, 11, 1);
531+
532+
memset(line, ' ', width);
533+
line[0] = line[width] = 0xBA;
534+
535+
for (int i = 1; i < height; i++) {
536+
draw_text(line, x, y + i, 11, 1);
537+
}
538+
539+
snprintf(line, width - 1, " %s ", title);
540+
draw_text(line, x + (width - strlen(line)) / 2, y, 14, 3);
541+
}
525542

526543
char* get_free_vram_ptr() {
527544
return text_buffer + text_buffer_width * 2 * text_buffer_height;
@@ -537,6 +554,7 @@ void logFile(char* msg);
537554

538555
extern volatile bool manager_started;
539556
void logMsg(char* msg) {
557+
return;
540558
#if BOOT_DEBUG
541559
{ char tmp[85]; sprintf(tmp, "%s\n", msg); logFile(tmp); }
542560
#else

‎drivers/vga-nextgen/vga.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
#define beginVGA_PIN (6)
88
#define VGA_DMA_IRQ (DMA_IRQ_0)
99

10+
#define TEXTMODE_COLS 80
11+
#define TEXTMODE_ROWS 30
12+
1013
enum graphics_mode_t {
1114
TEXTMODE_40x30,
1215
TEXTMODE_80x30,
@@ -41,7 +44,7 @@ void graphics_set_bgcolor(uint32_t color888);
4144

4245
void clrScr(uint8_t color);
4346

44-
void draw_text(char *string, int x, int y, uint8_t color, uint8_t bgcolor);
47+
void draw_text(const char string[TEXTMODE_COLS], uint32_t x, uint32_t y, uint8_t color, uint8_t bgcolor);
4548

4649
void logMsg(char * msg);
4750

‎src/cpu.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -727,9 +727,9 @@ static void intcall86(uint8_t intnum) {
727727
}
728728
// http://www.techhelpmanual.com/114-video_modes.html
729729
// http://www.techhelpmanual.com/89-video_memory_layouts.html
730-
char tmp[40];
731-
snprintf(tmp, 40, "VBIOS: Mode AH=0x%x (videomode: 0x%x)", CPU_AX, videomode);
732-
logMsg(tmp);
730+
// char tmp[40];
731+
// snprintf(tmp, 40, "VBIOS: Mode AH=0x%x (videomode: 0x%x)", CPU_AX, videomode);
732+
// logMsg(tmp);
733733
#if PICO_ON_DEVICE
734734
if (videomode <= 0xd) {
735735
graphics_set_buffer(VIDEORAM + 32768, 320, 200);

‎src/disk.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ static _FILE* tryFlushROM(uint8_t drivenum, size_t size, char *ROM, char *path)
8282
#include "fat12.h"
8383

8484
static _FILE* tryDefaultDrive(uint8_t drivenum, size_t size, char *path) {
85-
char* tmp[40];
86-
sprintf(tmp, "Drive 0x%02X not found. Will try to init %s by size: %f MB...", drivenum, path, (size / 1024 / 1024.0f));
87-
logMsg(tmp);
85+
// char* tmp[40];
86+
// sprintf(tmp, "Drive 0x%02X not found. Will try to init %s by size: %f MB...", drivenum, path, (size / 1024 / 1024.0f));
87+
// logMsg(tmp);
8888
_FILE *pFile = actualDrive(drivenum);
8989
FRESULT result = f_open(pFile, path, FA_WRITE | FA_CREATE_ALWAYS);
9090
if (result != FR_OK) {

‎src/ports.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -316,9 +316,9 @@ void portout(uint16_t portnum, uint16_t value) {
316316
uint8_t bg_color = value & 0xf;
317317
cga_colorset = value >> 5 & 1;
318318
cga_intensity = value >> 4 & 1;
319-
char tmp[80];
320-
snprintf(tmp, 80, "VIDEOMODE: colorset: %i intensity: %i value: %x", cga_colorset, cga_intensity, value);
321-
logMsg(tmp);
319+
// char tmp[80];
320+
// snprintf(tmp, 80, "VIDEOMODE: colorset: %i intensity: %i value: %x", cga_colorset, cga_intensity, value);
321+
// logMsg(tmp);
322322
#if PICO_ON_DEVICE
323323
graphics_set_palette(0, bg_color != 0xf ? cga_palette[bg_color] : 0);
324324

@@ -351,7 +351,7 @@ void portout(uint16_t portnum, uint16_t value) {
351351
if (videomode >= 0xd) return;
352352
// third cga palette (black/red/cyan/white)
353353
if (videomode == 5 && (port3D8 >> 2) & 1) {
354-
logMsg("the unofficial Mode 5 palette, accessed by disabling ColorBurst\n");
354+
// logMsg("the unofficial Mode 5 palette, accessed by disabling ColorBurst\n");
355355
cga_colorset = 2;
356356
#if PICO_ON_DEVICE
357357
for (int i = 0; i < 4; i++) {

‎src/ram_page.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ bool init_vram() {
171171
FRESULT vram_seek(FIL* fp, uint32_t file_offset) {
172172
FRESULT result = f_lseek(&file, file_offset);
173173
if (result != FR_OK) {
174-
char tmp[40];
174+
char tmp[80];
175175
result = f_open(&file, path, FA_READ | FA_WRITE);
176176
if (result != FR_OK) {
177177
sprintf(tmp, "Unable to open pagefile.sys: %s (%d)", FRESULT_str(result), result);
@@ -197,7 +197,7 @@ void read_vram_block(char* dst, uint32_t file_offset, uint32_t sz) {
197197
UINT br;
198198
result = f_read(&file, dst, sz, &br);
199199
if (result != FR_OK) {
200-
char tmp[40];
200+
char tmp[80];
201201
sprintf(tmp, "Failed to f_read: %s (%d)", FRESULT_str(result), result);
202202
logMsg(tmp);
203203
}
@@ -217,7 +217,7 @@ void flush_vram_block(const char* src, uint32_t file_offset, uint32_t sz) {
217217
UINT bw;
218218
result = f_write(&file, src, sz, &bw);
219219
if (result != FR_OK) {
220-
char tmp[40];
220+
char tmp[80];
221221
sprintf(tmp, "Failed to f_write: %s (%d)", FRESULT_str(result), result);
222222
logMsg(tmp);
223223
}

0 commit comments

Comments
 (0)
Please sign in to comment.