From fbe9e4ac10001f931b2f3b9898e3292d36e8cc7c Mon Sep 17 00:00:00 2001 From: Aidan MacDonald Date: Sat, 19 Mar 2022 13:54:25 +0000 Subject: x1000: bootloader: refactor splash/splash2 Allow the use of printf formatting and multiple lines of text using newlines in the string. Change-Id: I65919bf29c16c34c38cf3995e02d2ddbbaa4bdf3 --- bootloader/x1000/boot.c | 18 +++++----- bootloader/x1000/gui.c | 69 +++++++++++++++++++++++++++++++------- bootloader/x1000/install.c | 6 ++-- bootloader/x1000/main.c | 2 +- bootloader/x1000/utils.c | 22 ++++++------ bootloader/x1000/x1000bootloader.h | 4 +-- 6 files changed, 82 insertions(+), 39 deletions(-) (limited to 'bootloader') diff --git a/bootloader/x1000/boot.c b/bootloader/x1000/boot.c index d6dfd4a193..60fb864d19 100644 --- a/bootloader/x1000/boot.c +++ b/bootloader/x1000/boot.c @@ -43,14 +43,14 @@ void boot_rockbox(void) void shutdown(void) { - splash(HZ, "Shutting down"); + splashf(HZ, "Shutting down"); power_off(); while(1); } void reboot(void) { - splash(HZ, "Rebooting"); + splashf(HZ, "Rebooting"); system_reboot(); while(1); } @@ -70,13 +70,13 @@ static int read_linux_args(const char* filename) size_t max_size; int handle = core_alloc_maximum("args", &max_size, &buflib_ops_locked); if(handle <= 0) { - splash(5*HZ, "Out of memory"); + splashf(5*HZ, "Out of memory"); return -2; } int fd = open(filename, O_RDONLY); if(fd < 0) { - splash2(5*HZ, "Can't open args file", filename); + splashf(5*HZ, "Can't open args file\n%s", filename); ret = -3; goto err_free; } @@ -84,7 +84,7 @@ static int read_linux_args(const char* filename) /* this isn't 100% correct but will be good enough */ off_t fsize = filesize(fd); if(fsize < 0 || fsize+1 > (off_t)max_size) { - splash(5*HZ, "Arguments too long"); + splashf(5*HZ, "Arguments too long"); ret = -4; goto err_close; } @@ -96,7 +96,7 @@ static int read_linux_args(const char* filename) close(fd); if(rdres != (ssize_t)fsize) { - splash(5*HZ, "Can't read args file"); + splashf(5*HZ, "Can't read args file"); ret = -5; goto err_free; } @@ -191,7 +191,7 @@ void boot_of_helper(uint32_t addr, uint32_t flash_size, const char* args) void* jump_addr = core_get_data(handle); uint32_t entry_addr = mips_linux_stub_get_entry(&jump_addr, img_length); if(entry_addr >= 0xa0000000 || entry_addr < 0x80000000) { - splash2(5*HZ, "Kernel patch failed", "Please send bugreport"); + splashf(5*HZ, "Kernel patch failed\nPlease send bugreport"); return; } @@ -216,7 +216,7 @@ void boot_of_player(void) #if defined(OF_PLAYER_ADDR) boot_of_helper(OF_PLAYER_ADDR, OF_PLAYER_LENGTH, OF_PLAYER_ARGS); #else - splash(HZ, "Not supported"); + splashf(HZ, "Not supported"); #endif } @@ -225,6 +225,6 @@ void boot_of_recovery(void) #if defined(OF_RECOVERY_ADDR) boot_of_helper(OF_RECOVERY_ADDR, OF_RECOVERY_LENGTH, OF_RECOVERY_ARGS); #else - splash(HZ, "Not supported"); + splashf(HZ, "Not supported"); #endif } diff --git a/bootloader/x1000/gui.c b/bootloader/x1000/gui.c index a672c30380..513a3cb9cb 100644 --- a/bootloader/x1000/gui.c +++ b/bootloader/x1000/gui.c @@ -29,6 +29,8 @@ #include "button.h" #include "version.h" #include +#include +#include static bool lcd_inited = false; extern bool is_usb_connected; @@ -53,27 +55,70 @@ void putcenter_y(int y, const char* msg) lcd_putsxy(x, y, msg); } -void putcenter_line(int line, const char* msg) +static void get_splash_size(const char* str, int* width, int* height) { - int y = LCD_HEIGHT/2 + (line - 1)*SYSFONT_HEIGHT; - putcenter_y(y, msg); + *width = 0; + *height = 0; + + while(str) { + const char* np = strchr(str, '\n'); + int len; + if(np) { + len = np - str; + np++; + } else { + len = strlen(str); + } + + *width = MAX(len, *width); + *height += 1; + str = np; + } } -void splash2(long delay, const char* msg, const char* msg2) +void splashf(long delay, const char* msg, ...) { + static char buf[512]; + + va_list ap; + va_start(ap, msg); + int len = vsnprintf(buf, sizeof(buf), msg, ap); + va_end(ap); + + if(len < 0) + return; + + int xpos, ypos; + int width, height; + get_splash_size(buf, &width, &height); + + width *= SYSFONT_WIDTH; + height *= SYSFONT_HEIGHT; + xpos = (LCD_WIDTH - width) / 2; + ypos = (LCD_HEIGHT - height) / 2; + + const int padding = 10; clearscreen(); - putcenter_line(0, msg); - if(msg2) - putcenter_line(1, msg2); + lcd_drawrect(xpos-padding, ypos-padding, + width+2*padding, height+2*padding); + + char* str = buf; + do { + char* np = strchr(str, '\n'); + if(np) { + *np = '\0'; + np++; + } + + lcd_putsxyf(xpos, ypos, "%s", str); + ypos += SYSFONT_HEIGHT; + str = np; + } while(str); + lcd_update(); sleep(delay); } -void splash(long delay, const char* msg) -{ - splash2(delay, msg, NULL); -} - int get_button(int timeout) { int btn = button_get_w_tmo(timeout); diff --git a/bootloader/x1000/install.c b/bootloader/x1000/install.c index e4af66443b..18b853df7c 100644 --- a/bootloader/x1000/install.c +++ b/bootloader/x1000/install.c @@ -34,7 +34,7 @@ enum { static void bootloader_action(int which) { if(check_disk(true) != DISK_PRESENT) { - splash2(5*HZ, "Install aborted", "Cannot access SD card"); + splashf(5*HZ, "Install aborted\nCannot access SD card"); return; } @@ -46,7 +46,7 @@ static void bootloader_action(int which) default: return; /* can't happen */ } - splash(0, msg); + splashf(0, msg); int rc; switch(which) { @@ -60,7 +60,7 @@ static void bootloader_action(int which) snprintf(buf, sizeof(buf), "%s (%d)", installer_strerror(rc), rc); const char* msg1 = rc == 0 ? "Success" : buf; const char* msg2 = "Press " BL_QUIT_NAME " to continue"; - splash2(0, msg1, msg2); + splashf(0, "%s\n%s", msg1, msg2); while(get_button(TIMEOUT_BLOCK) != BL_QUIT); } diff --git a/bootloader/x1000/main.c b/bootloader/x1000/main.c index c507b1d2c9..0909a4c72a 100644 --- a/bootloader/x1000/main.c +++ b/bootloader/x1000/main.c @@ -44,7 +44,7 @@ void main(void) enable_irq(); if(storage_init() < 0) { - splash(5*HZ, "storage_init() failed"); + splashf(5*HZ, "storage_init() failed"); power_off(); } diff --git a/bootloader/x1000/utils.c b/bootloader/x1000/utils.c index faeab40739..681ddbbd62 100644 --- a/bootloader/x1000/utils.c +++ b/bootloader/x1000/utils.c @@ -46,13 +46,13 @@ int check_disk(bool wait) return DISK_ABSENT; while(!storage_present(IF_MD(0))) { - splash2(0, "Insert SD card", "Press " BL_QUIT_NAME " to cancel"); + splashf(0, "Insert SD card\nPress " BL_QUIT_NAME " to cancel"); if(get_button(HZ/4) == BL_QUIT) return DISK_CANCELED; } /* a lie intended to give time for mounting the disk in the background */ - splash(HZ, "Scanning disk"); + splashf(HZ, "Scanning disk"); return DISK_PRESENT; } @@ -60,19 +60,19 @@ int check_disk(bool wait) void usb_mode(void) { if(!is_usb_connected) - splash2(0, "Waiting for USB", "Press " BL_QUIT_NAME " to cancel"); + splashf(0, "Waiting for USB\nPress " BL_QUIT_NAME " to cancel"); while(!is_usb_connected) if(get_button(TIMEOUT_BLOCK) == BL_QUIT) return; - splash(0, "USB mode"); + splashf(0, "USB mode"); usb_acknowledge(SYS_USB_CONNECTED_ACK); while(is_usb_connected) get_button(TIMEOUT_BLOCK); - splash(3*HZ, "USB disconnected"); + splashf(3*HZ, "USB disconnected"); } int load_rockbox(const char* filename, size_t* sizep) @@ -82,7 +82,7 @@ int load_rockbox(const char* filename, size_t* sizep) int handle = core_alloc_maximum("rockbox", sizep, &buflib_ops_locked); if(handle < 0) { - splash(5*HZ, "Out of memory"); + splashf(5*HZ, "Out of memory"); return -2; } @@ -90,7 +90,7 @@ int load_rockbox(const char* filename, size_t* sizep) int rc = load_firmware(loadbuffer, filename, *sizep); if(rc <= 0) { core_free(handle); - splash2(5*HZ, "Error loading Rockbox", loader_strerror(rc)); + splashf(5*HZ, "Error loading Rockbox\n%s", loader_strerror(rc)); return -3; } @@ -108,13 +108,13 @@ int load_uimage_file(const char* filename, int fd = open(filename, O_RDONLY); if(fd < 0) { - splash2(5*HZ, "Can't open file", filename); + splashf(5*HZ, "Can't open file\n%s", filename); return -2; } int handle = uimage_load(uh, sizep, uimage_fd_reader, (void*)(intptr_t)fd); if(handle <= 0) { - splash2(5*HZ, "Cannot load uImage", filename); + splashf(5*HZ, "Cannot load uImage\n%s", filename); return -3; } @@ -155,7 +155,7 @@ int load_uimage_flash(uint32_t addr, uint32_t length, nand_lock(n.ndrv); if(nand_open(n.ndrv) != NAND_SUCCESS) { - splash(5*HZ, "NAND open failed"); + splashf(5*HZ, "NAND open failed"); nand_unlock(n.ndrv); return -1; } @@ -166,7 +166,7 @@ int load_uimage_flash(uint32_t addr, uint32_t length, nand_unlock(n.ndrv); if(handle <= 0) { - splash(5*HZ, "uImage load failed"); + splashf(5*HZ, "uImage load failed"); return -2; } diff --git a/bootloader/x1000/x1000bootloader.h b/bootloader/x1000/x1000bootloader.h index b0d8d378f4..0309421ced 100644 --- a/bootloader/x1000/x1000bootloader.h +++ b/bootloader/x1000/x1000bootloader.h @@ -118,9 +118,7 @@ struct bl_list { void clearscreen(void); void putversion(void); void putcenter_y(int y, const char* msg); -void putcenter_line(int line, const char* msg); -void splash2(long delay, const char* msg, const char* msg2); -void splash(long delay, const char* msg); +void splashf(long delay, const char* msg, ...); int get_button(int timeout); void init_lcd(void); -- cgit v1.2.3