From 77595c6d9e2c1af9c3b143f496c4c47d0e95a458 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sun, 10 Nov 2013 10:26:57 -0700 Subject: sandbox: Improve/augment memory allocation functions Implement realloc() and free() for sandbox, by adding a header to each block which contains the block size. Signed-off-by: Simon Glass Signed-off-by: Simon Glass Reviewed-by: Che-Liang Chiou Reviewed-by: Hung-ying Tyan --- include/os.h | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'include/os.h') diff --git a/include/os.h b/include/os.h index 950433daa32..1575a969222 100644 --- a/include/os.h +++ b/include/os.h @@ -106,6 +106,35 @@ void os_tty_raw(int fd); */ void *os_malloc(size_t length); +/** + * Free memory previous allocated with os_malloc()/os_realloc() + * + * This returns the memory to the OS. + * + * \param ptr Pointer to memory block to free + */ +void *os_free(void *ptr); + +/** + * Reallocate previously-allocated memory to increase/decrease space + * + * This works in a similar way to the C library realloc() function. If + * length is 0, then ptr is freed. Otherwise the space used by ptr is + * expanded or reduced depending on whether length is larger or smaller + * than before. + * + * If ptr is NULL, then this is similar to calling os_malloc(). + * + * This function may need to move the memory block to make room for any + * extra space, in which case the new pointer is returned. + * + * \param ptr Pointer to memory block to reallocate + * \param length New length for memory block + * \return pointer to new memory block, or NULL on failure or if length + * is 0. + */ +void *os_realloc(void *ptr, size_t length); + /** * Access to the usleep function of the os * -- cgit v1.2.3 From 91b136c7989e763b01632ca3de6fca8ead0b847b Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sun, 10 Nov 2013 10:27:01 -0700 Subject: sandbox: Allow the console to work earlier With sandbox, errors and problems may be reported before console_init_f() is executed. For example, an argument may not parse correctly or U-Boot may panic(). At present this output is swallowed so there is no indication what is going wrong. Adjust the console to deal with a very early sandbox setup, by detecting that there is no global_data yet, and calling os functions in that case. Signed-off-by: Simon Glass Signed-off-by: Simon Glass --- include/os.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'include/os.h') diff --git a/include/os.h b/include/os.h index 1575a969222..d302b3685bb 100644 --- a/include/os.h +++ b/include/os.h @@ -209,4 +209,24 @@ const char *os_dirent_get_typename(enum os_dirent_t type); */ ssize_t os_get_filesize(const char *fname); +/** + * Write a character to the controlling OS terminal + * + * This bypasses the U-Boot console support and writes directly to the OS + * stdout file descriptor. + * + * @param ch Character to write + */ +void os_putc(int ch); + +/** + * Write a string to the controlling OS terminal + * + * This bypasses the U-Boot console support and writes directly to the OS + * stdout file descriptor. + * + * @param str String to write (note that \n is not appended) + */ +void os_puts(const char *str); + #endif -- cgit v1.2.3 From 5c2859cdc30287b3593d9df88f48c31eecb0bbed Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sun, 10 Nov 2013 10:27:03 -0700 Subject: sandbox: Allow reading/writing of RAM buffer It is useful to be able to save and restore the RAM contents of sandbox U-Boot either for setting up tests, for later analysys, or for chaining together multiple tests which need to keep the same memory contents. Add a function to provide a memory file for U-Boot. This is read on start-up and written when shutting down. If the file does not exist on start-up, it will be created when shutting down. Signed-off-by: Simon Glass Signed-off-by: Simon Glass --- include/os.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'include/os.h') diff --git a/include/os.h b/include/os.h index d302b3685bb..b65fba43014 100644 --- a/include/os.h +++ b/include/os.h @@ -229,4 +229,20 @@ void os_putc(int ch); */ void os_puts(const char *str); +/** + * Write the sandbox RAM buffer to a existing file + * + * @param fname Filename to write memory to (simple binary format) + * @return 0 if OK, -ve on error + */ +int os_write_ram_buf(const char *fname); + +/** + * Read the sandbox RAM buffer from an existing file + * + * @param fname Filename containing memory (simple binary format) + * @return 0 if OK, -ve on error + */ +int os_read_ram_buf(const char *fname); + #endif -- cgit v1.2.3