Skip to content

Commit

Permalink
buffer overflow fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
xrip committed Feb 12, 2024
1 parent f1f8bb2 commit 143b564
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 38 deletions.
9 changes: 5 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${OUTPUT_DIR}")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${OUTPUT_DIR}")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${OUTPUT_DIR}")

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

if (PICO_PLATFORM STREQUAL "host")
set(SDL2_LIB_DIR C:/SDL/lib)
Expand Down Expand Up @@ -130,12 +130,13 @@ else ()
hardware_clocks
hardware_pwm
hardware_flash

tinyusb_board
tinyusb_device
)
target_link_options(${PROJECT_NAME} PRIVATE "LINKER:--script=${CMAKE_CURRENT_LIST_DIR}/memmap.ld")
set_target_properties(${PROJECT_NAME} PROPERTIES LINK_DEPENDS ${CMAKE_CURRENT_LIST_DIR}/memmap.ld)

family_configure_device_example(${PROJECT_NAME} noos)

target_sources(${PROJECT_NAME} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/drivers/usb/usb.c
${CMAKE_CURRENT_SOURCE_DIR}/drivers/usb/msc_disk.c
Expand Down
4 changes: 2 additions & 2 deletions drivers/usb/msc_disk.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ void tud_msc_capacity_cb(uint8_t lun, uint32_t* block_count, uint16_t* block_siz
if (dio == RES_OK) {
*block_count = dw;
} else {
char tmp[80]; sprintf(tmp, "disk_ioctl(GET_SECTOR_COUNT) failed: %d", dio); logMsg(tmp);
// char tmp[80]; sprintf(tmp, "disk_ioctl(GET_SECTOR_COUNT) failed: %d", dio); logMsg(tmp);
*block_count = 0;
return;
}
Expand All @@ -175,7 +175,7 @@ void tud_msc_capacity_cb(uint8_t lun, uint32_t* block_count, uint16_t* block_siz
// - Start = 0 : stopped power mode, if load_eject = 1 : unload disk storage
// - Start = 1 : active mode, if load_eject = 1 : load disk storage
bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, bool load_eject) {
char tmp[81]; sprintf(tmp, "power_condition: 0x%X start: %d load_eject: %d", power_condition, start, load_eject); logMsg(tmp);
// char tmp[81]; sprintf(tmp, "power_condition: 0x%X start: %d load_eject: %d", power_condition, start, load_eject); logMsg(tmp);
(void) lun;
(void) power_condition;
if ( load_eject ) {
Expand Down
54 changes: 36 additions & 18 deletions drivers/vga-nextgen/vga.c
Original file line number Diff line number Diff line change
Expand Up @@ -502,26 +502,43 @@ void clrScr(uint8_t color) {
current_line = start_debug_line;
};

void draw_text(char* string, int x, int y, uint8_t color, uint8_t bgcolor) {
/*if ((y < 0) | (y >= text_buffer_height)) return;
if (x >= text_buffer_width) return;
int len = strlen(string);
if (x < 0) {
if ((len + x) > 0) {
string += -x;
x = 0;
}
else {
return;
}
}*/
uint8_t* t_buf = text_buffer + text_buffer_width * 2 * y + 2 * x;
for (int xi = x; xi < text_buffer_width * 2; xi++) {
if (!(*string)) break;
void draw_text(const char string[TEXTMODE_COLS + 1], uint32_t x, uint32_t y, uint8_t color, uint8_t bgcolor) {
uint8_t* t_buf = text_buffer + TEXTMODE_COLS * 2 * y + 2 * x;
for (int xi = TEXTMODE_COLS * 2; xi--;) {
if (!*string) break;
*t_buf++ = *string++;
*t_buf++ = (bgcolor << 4) | (color & 0xF);
*t_buf++ = bgcolor << 4 | color & 0xF;
}
};
}

void draw_window(const char title[TEXTMODE_COLS + 1], uint32_t x, uint32_t y, uint32_t width, uint32_t height) {
char line[width + 1];
memset(line, 0, sizeof line);
width--;
height--;
// Рисуем рамки

memset(line, 0xCD, width); // ═══


line[0] = 0xC9; // ╔
line[width] = 0xBB; // ╗
draw_text(line, x, y, 11, 1);

line[0] = 0xC8; // ╚
line[width] = 0xBC; // ╝
draw_text(line, x, height + y, 11, 1);

memset(line, ' ', width);
line[0] = line[width] = 0xBA;

for (int i = 1; i < height; i++) {
draw_text(line, x, y + i, 11, 1);
}

snprintf(line, width - 1, " %s ", title);
draw_text(line, x + (width - strlen(line)) / 2, y, 14, 3);
}

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

extern volatile bool manager_started;
void logMsg(char* msg) {
return;
#if BOOT_DEBUG
{ char tmp[85]; sprintf(tmp, "%s\n", msg); logFile(tmp); }
#else
Expand Down
5 changes: 4 additions & 1 deletion drivers/vga-nextgen/vga.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#define beginVGA_PIN (6)
#define VGA_DMA_IRQ (DMA_IRQ_0)

#define TEXTMODE_COLS 80
#define TEXTMODE_ROWS 30

enum graphics_mode_t {
TEXTMODE_40x30,
TEXTMODE_80x30,
Expand Down Expand Up @@ -41,7 +44,7 @@ void graphics_set_bgcolor(uint32_t color888);

void clrScr(uint8_t color);

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

void logMsg(char * msg);

Expand Down
6 changes: 3 additions & 3 deletions src/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -727,9 +727,9 @@ static void intcall86(uint8_t intnum) {
}
// http://www.techhelpmanual.com/114-video_modes.html
// http://www.techhelpmanual.com/89-video_memory_layouts.html
char tmp[40];
snprintf(tmp, 40, "VBIOS: Mode AH=0x%x (videomode: 0x%x)", CPU_AX, videomode);
logMsg(tmp);
// char tmp[40];
// snprintf(tmp, 40, "VBIOS: Mode AH=0x%x (videomode: 0x%x)", CPU_AX, videomode);
// logMsg(tmp);
#if PICO_ON_DEVICE
if (videomode <= 0xd) {
graphics_set_buffer(VIDEORAM + 32768, 320, 200);
Expand Down
6 changes: 3 additions & 3 deletions src/disk.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ static _FILE* tryFlushROM(uint8_t drivenum, size_t size, char *ROM, char *path)
#include "fat12.h"

static _FILE* tryDefaultDrive(uint8_t drivenum, size_t size, char *path) {
char* tmp[40];
sprintf(tmp, "Drive 0x%02X not found. Will try to init %s by size: %f MB...", drivenum, path, (size / 1024 / 1024.0f));
logMsg(tmp);
// char* tmp[40];
// sprintf(tmp, "Drive 0x%02X not found. Will try to init %s by size: %f MB...", drivenum, path, (size / 1024 / 1024.0f));
// logMsg(tmp);
_FILE *pFile = actualDrive(drivenum);
FRESULT result = f_open(pFile, path, FA_WRITE | FA_CREATE_ALWAYS);
if (result != FR_OK) {
Expand Down
8 changes: 4 additions & 4 deletions src/ports.c
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,9 @@ void portout(uint16_t portnum, uint16_t value) {
uint8_t bg_color = value & 0xf;
cga_colorset = value >> 5 & 1;
cga_intensity = value >> 4 & 1;
char tmp[80];
snprintf(tmp, 80, "VIDEOMODE: colorset: %i intensity: %i value: %x", cga_colorset, cga_intensity, value);
logMsg(tmp);
// char tmp[80];
// snprintf(tmp, 80, "VIDEOMODE: colorset: %i intensity: %i value: %x", cga_colorset, cga_intensity, value);
// logMsg(tmp);
#if PICO_ON_DEVICE
graphics_set_palette(0, bg_color != 0xf ? cga_palette[bg_color] : 0);

Expand Down Expand Up @@ -351,7 +351,7 @@ void portout(uint16_t portnum, uint16_t value) {
if (videomode >= 0xd) return;
// third cga palette (black/red/cyan/white)
if (videomode == 5 && (port3D8 >> 2) & 1) {
logMsg("the unofficial Mode 5 palette, accessed by disabling ColorBurst\n");
// logMsg("the unofficial Mode 5 palette, accessed by disabling ColorBurst\n");
cga_colorset = 2;
#if PICO_ON_DEVICE
for (int i = 0; i < 4; i++) {
Expand Down
6 changes: 3 additions & 3 deletions src/ram_page.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ bool init_vram() {
FRESULT vram_seek(FIL* fp, uint32_t file_offset) {
FRESULT result = f_lseek(&file, file_offset);
if (result != FR_OK) {
char tmp[40];
char tmp[80];
result = f_open(&file, path, FA_READ | FA_WRITE);
if (result != FR_OK) {
sprintf(tmp, "Unable to open pagefile.sys: %s (%d)", FRESULT_str(result), result);
Expand All @@ -197,7 +197,7 @@ void read_vram_block(char* dst, uint32_t file_offset, uint32_t sz) {
UINT br;
result = f_read(&file, dst, sz, &br);
if (result != FR_OK) {
char tmp[40];
char tmp[80];
sprintf(tmp, "Failed to f_read: %s (%d)", FRESULT_str(result), result);
logMsg(tmp);
}
Expand All @@ -217,7 +217,7 @@ void flush_vram_block(const char* src, uint32_t file_offset, uint32_t sz) {
UINT bw;
result = f_write(&file, src, sz, &bw);
if (result != FR_OK) {
char tmp[40];
char tmp[80];
sprintf(tmp, "Failed to f_write: %s (%d)", FRESULT_str(result), result);
logMsg(tmp);
}
Expand Down

0 comments on commit 143b564

Please sign in to comment.