diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/Kconfig | 1 | ||||
-rw-r--r-- | common/Makefile | 4 | ||||
-rw-r--r-- | common/bootchooser.c | 72 | ||||
-rw-r--r-- | common/calloc.c | 19 | ||||
-rw-r--r-- | common/console.c | 4 | ||||
-rw-r--r-- | common/dummy_malloc.c | 22 | ||||
-rw-r--r-- | common/partitions/efi.c | 2 | ||||
-rw-r--r-- | common/ratp/ratp.c | 2 | ||||
-rw-r--r-- | common/state/backend_bucket_circular.c | 13 | ||||
-rw-r--r-- | common/state/state.c | 7 | ||||
-rw-r--r-- | common/state/state_variables.c | 6 | ||||
-rw-r--r-- | common/tlsf_malloc.c | 33 |
12 files changed, 113 insertions, 72 deletions
diff --git a/common/Kconfig b/common/Kconfig index 4e5f4a72ee..2ad92158c1 100644 --- a/common/Kconfig +++ b/common/Kconfig @@ -660,7 +660,6 @@ config BOOTM_FORCE_SIGNED_IMAGES since they are the only supported image type that support signing. config BLSPEC - depends on BLOCK depends on FLEXIBLE_BOOTARGS depends on !SHELL_NONE select BOOT diff --git a/common/Makefile b/common/Makefile index 2b0f4cc988..b617642242 100644 --- a/common/Makefile +++ b/common/Makefile @@ -34,8 +34,8 @@ obj-$(CONFIG_GLOBALVAR) += globalvar.o obj-$(CONFIG_GREGORIAN_CALENDER) += date.o obj-$(CONFIG_KALLSYMS) += kallsyms.o obj-$(CONFIG_MALLOC_DLMALLOC) += dlmalloc.o -obj-$(CONFIG_MALLOC_TLSF) += tlsf_malloc.o tlsf.o -obj-$(CONFIG_MALLOC_DUMMY) += dummy_malloc.o +obj-$(CONFIG_MALLOC_TLSF) += tlsf_malloc.o tlsf.o calloc.o +obj-$(CONFIG_MALLOC_DUMMY) += dummy_malloc.o calloc.o obj-$(CONFIG_MEMINFO) += meminfo.o obj-$(CONFIG_MENU) += menu.o obj-$(CONFIG_MODULES) += module.o diff --git a/common/bootchooser.c b/common/bootchooser.c index 83b15e0f78..c47c734c9c 100644 --- a/common/bootchooser.c +++ b/common/bootchooser.c @@ -52,12 +52,12 @@ static int retry; static int last_boot_successful; struct bootchooser { - struct bootentry entry; struct list_head targets; struct bootchooser_target *last_chosen; struct state *state; char *state_prefix; + int refs; int verbose; int dryrun; @@ -345,11 +345,15 @@ static void bootchooser_reset_priorities(struct bootchooser *bc) bootchooser_target_set_priority(target, -1); } +static struct bootchooser *bootchooser; + /** - * bootchooser_get - get a bootchooser instance + * bootchooser_get - get a reference to the bootchooser * - * This evaluates the different globalvars and eventually state variables, - * creates a bootchooser instance from it and returns it. + * When no bootchooser is initialized this function allocates the bootchooser + * and initializes it with the different globalvars and state variables. The + * bootchooser is returned. Subsequent calls will return a reference to the same + * bootchooser. */ struct bootchooser *bootchooser_get(void) { @@ -360,6 +364,11 @@ struct bootchooser *bootchooser_get(void) uint32_t last_chosen; static int attempts_resetted; + if (bootchooser) { + bootchooser->refs++; + return bootchooser; + } + bc = xzalloc(sizeof(*bc)); if (*state_prefix) { @@ -474,6 +483,10 @@ struct bootchooser *bootchooser_get(void) } + bootchooser = bc; + + bootchooser->refs = 1; + return bc; err: @@ -529,16 +542,26 @@ int bootchooser_save(struct bootchooser *bc) } /** - * bootchooser_put - release a bootchooser instance + * bootchooser_put - return a bootchooser reference * @bc: The bootchooser instance * - * This releases a bootchooser instance and the memory associated with it. + * This returns a reference to the bootchooser. If it is the last reference the + * bootchooser is saved and the associated memory is freed. + * + * Return: 0 for success or a negative error code. An error can occur when + * bootchooser_save fails to write to the storage, nevertheless the + * bootchooser reference is still released. */ int bootchooser_put(struct bootchooser *bc) { struct bootchooser_target *target, *tmp; int ret; + bc->refs--; + + if (bc->refs) + return 0; + ret = bootchooser_save(bc); if (ret) pr_err("Failed to save bootchooser state: %s\n", strerror(-ret)); @@ -553,6 +576,8 @@ int bootchooser_put(struct bootchooser *bc) free(bc); + bootchooser = NULL; + return ret; } @@ -841,20 +866,26 @@ int bootchooser_boot(struct bootchooser *bc) static int bootchooser_entry_boot(struct bootentry *entry, int verbose, int dryrun) { - struct bootchooser *bc = container_of(entry, struct bootchooser, - entry); + struct bootchooser *bc; + int ret; + + bc = bootchooser_get(); + if (IS_ERR(bc)) + return PTR_ERR(bc); + bc->verbose = verbose; bc->dryrun = dryrun; - return bootchooser_boot(bc); + ret = bootchooser_boot(bc); + + bootchooser_put(bc); + + return ret; } static void bootchooser_release(struct bootentry *entry) { - struct bootchooser *bc = container_of(entry, struct bootchooser, - entry); - - bootchooser_put(bc); + free(entry); } /** @@ -869,6 +900,7 @@ static void bootchooser_release(struct bootentry *entry) static int bootchooser_add_entry(struct bootentries *entries, const char *name) { struct bootchooser *bc; + struct bootentry *entry; if (strcmp(name, "bootchooser")) return 0; @@ -877,12 +909,16 @@ static int bootchooser_add_entry(struct bootentries *entries, const char *name) if (IS_ERR(bc)) return PTR_ERR(bc); - bc->entry.boot = bootchooser_entry_boot; - bc->entry.release = bootchooser_release; - bc->entry.title = xstrdup("bootchooser"); - bc->entry.description = xstrdup("bootchooser"); + entry = xzalloc(sizeof(*entry)); - bootentries_add_entry(entries, &bc->entry); + entry->boot = bootchooser_entry_boot; + entry->release = bootchooser_release; + entry->title = xstrdup("bootchooser"); + entry->description = xstrdup("bootchooser"); + + bootentries_add_entry(entries, entry); + + bootchooser_put(bc); return 1; } diff --git a/common/calloc.c b/common/calloc.c new file mode 100644 index 0000000000..2b933ec272 --- /dev/null +++ b/common/calloc.c @@ -0,0 +1,19 @@ +#include <common.h> +#include <malloc.h> + +/* + * calloc calls malloc, then zeroes out the allocated chunk. + */ +void *calloc(size_t n, size_t elem_size) +{ + size_t size = elem_size * n; + void *r = malloc(size); + + if (!r) + return r; + + memset(r, 0x0, size); + + return r; +} +EXPORT_SYMBOL(calloc); diff --git a/common/console.c b/common/console.c index 40c26b66d9..47ccf2e54d 100644 --- a/common/console.c +++ b/common/console.c @@ -314,10 +314,10 @@ int console_register(struct console_device *newcdev) if (newcdev->devname) { dev->id = newcdev->devid; - strcpy(dev->name, newcdev->devname); + dev_set_name(dev, newcdev->devname); } else { dev->id = DEVICE_ID_DYNAMIC; - strcpy(dev->name, "cs"); + dev_set_name(dev, "cs"); } if (newcdev->dev) diff --git a/common/dummy_malloc.c b/common/dummy_malloc.c index 641baa125a..0120d9be2e 100644 --- a/common/dummy_malloc.c +++ b/common/dummy_malloc.c @@ -30,11 +30,14 @@ void malloc_stats(void) void *memalign(size_t alignment, size_t bytes) { - unsigned long mem = (unsigned long)sbrk(bytes + alignment); + void *mem = sbrk(bytes + alignment); - mem = (mem + alignment) & ~(alignment - 1); + if (!mem) { + errno = ENOMEM; + return NULL; + } - return (void *)mem; + return PTR_ALIGN(mem, alignment); } void *malloc(size_t size) @@ -50,16 +53,3 @@ void *realloc(void *ptr, size_t size) { BUG(); } - -void *calloc(size_t n, size_t elem_size) -{ - size_t size = elem_size * n; - void *r = malloc(size); - - if (!r) - return r; - - memset(r, 0x0, size); - - return r; -} diff --git a/common/partitions/efi.c b/common/partitions/efi.c index 88734f166b..3c1077fd0c 100644 --- a/common/partitions/efi.c +++ b/common/partitions/efi.c @@ -23,7 +23,7 @@ #include "efi.h" #include "parser.h" -static int force_gpt = IS_ENABLED(CONFIG_PARTITION_DISK_EFI_GPT_NO_FORCE); +static const int force_gpt = IS_ENABLED(CONFIG_PARTITION_DISK_EFI_GPT_NO_FORCE); /** * efi_crc32() - EFI version of crc32 function diff --git a/common/ratp/ratp.c b/common/ratp/ratp.c index fae9cec5b9..9aea1786d6 100644 --- a/common/ratp/ratp.c +++ b/common/ratp/ratp.c @@ -72,7 +72,7 @@ int register_ratp_command(struct ratp_command *cmd) } EXPORT_SYMBOL(register_ratp_command); -struct ratp_command *find_ratp_request(uint16_t request_id) +static struct ratp_command *find_ratp_request(uint16_t request_id) { struct ratp_command *cmdtp; diff --git a/common/state/backend_bucket_circular.c b/common/state/backend_bucket_circular.c index 0529421a2c..4b71d8751d 100644 --- a/common/state/backend_bucket_circular.c +++ b/common/state/backend_bucket_circular.c @@ -155,8 +155,6 @@ static int state_mtd_peb_read(struct state_backend_storage_bucket_circular *circ { int ret; off_t offset = suboffset; - struct mtd_ecc_stats stat1, stat2; - bool nostats = false; offset += (off_t)circ->eraseblock * circ->mtd->erasesize; @@ -480,7 +478,8 @@ int state_backend_bucket_circular_create(struct device_d *dev, const char *path, circ->fd = open(path, O_RDWR); if (circ->fd < 0) { pr_err("Failed to open circular bucket '%s'\n", path); - return -errno; + ret = -errno; + goto out_free; } #endif @@ -489,7 +488,7 @@ int state_backend_bucket_circular_create(struct device_d *dev, const char *path, dev_info(dev, "Not using eraseblock %u, it is marked as bad (%d)\n", circ->eraseblock, ret); ret = -EIO; - goto out_free; + goto out_close; } circ->bucket.read = state_backend_bucket_circular_read; @@ -499,13 +498,15 @@ int state_backend_bucket_circular_create(struct device_d *dev, const char *path, ret = state_backend_bucket_circular_init(*bucket); if (ret) - goto out_free; + goto out_close; return 0; -out_free: +out_close: #ifndef __BAREBOX__ close(circ->fd); +out_free: + free(circ->mtd); #endif free(circ); diff --git a/common/state/state.c b/common/state/state.c index 25d9502111..54c57232e6 100644 --- a/common/state/state.c +++ b/common/state/state.c @@ -83,7 +83,7 @@ out: } /** - * state_load - Loads a state from the backend + * state_do_load - Loads a state from the backend * @param state The state that should be updated to contain the loaded data * @return 0 on success, -errno on failure. If no state is loaded the previous * values remain in the state. @@ -179,7 +179,7 @@ static struct state *state_new(const char *name) int ret; state = xzalloc(sizeof(*state)); - safe_strncpy(state->dev.name, name, MAX_DRIVER_NAME); + dev_set_name(&state->dev, name); state->name = state->dev.name; state->dev.id = DEVICE_ID_SINGLE; INIT_LIST_HEAD(&state->variables); @@ -311,7 +311,7 @@ static int state_convert_node_variable(struct state *state, if ((conv == STATE_CONVERT_TO_NODE) || (conv == STATE_CONVERT_FIXUP)) { ret = of_property_write_string(new_node, "type", - vtype->type_name); + vtype->type_name); if (ret) goto out; @@ -583,6 +583,7 @@ void state_release(struct state *state) /* * state_new_from_node - create a new state instance from a device_node * + * @node The device_node describing the new state instance * @readonly This is a read-only state. Note that with this option set, * there are no repairs done. */ diff --git a/common/state/state_variables.c b/common/state/state_variables.c index de9ba4ab61..abd714ceda 100644 --- a/common/state/state_variables.c +++ b/common/state/state_variables.c @@ -339,8 +339,7 @@ static int state_string_export(struct state_variable *var, int ret = 0; if (string->value_default) { - ret = of_set_property(node, "default", string->value_default, - strlen(string->value_default) + 1, 1); + ret = of_property_write_string(node, "default", string->value_default); if (ret) return ret; @@ -350,8 +349,7 @@ static int state_string_export(struct state_variable *var, return 0; if (string->value) - ret = of_set_property(node, "value", string->value, - strlen(string->value) + 1, 1); + ret = of_property_write_string(node, "value", string->value); return ret; } diff --git a/common/tlsf_malloc.c b/common/tlsf_malloc.c index a3541d8256..c8900fc6bb 100644 --- a/common/tlsf_malloc.c +++ b/common/tlsf_malloc.c @@ -28,6 +28,7 @@ extern tlsf_pool tlsf_mem_pool; void *malloc(size_t bytes) { + void *mem; /* * tlsf_malloc returns NULL for zero bytes, we instead want * to have a valid pointer. @@ -35,25 +36,13 @@ void *malloc(size_t bytes) if (!bytes) bytes = 1; - return tlsf_malloc(tlsf_mem_pool, bytes); -} -EXPORT_SYMBOL(malloc); - -/* - * calloc calls malloc, then zeroes out the allocated chunk. - */ -void *calloc(size_t n, size_t elem_size) -{ - void *mem; - size_t sz; - - sz = n * elem_size; - mem = malloc(sz); - memset(mem, 0, sz); + mem = tlsf_malloc(tlsf_mem_pool, bytes); + if (!mem) + errno = ENOMEM; return mem; } -EXPORT_SYMBOL(calloc); +EXPORT_SYMBOL(malloc); void free(void *mem) { @@ -63,13 +52,21 @@ EXPORT_SYMBOL(free); void *realloc(void *oldmem, size_t bytes) { - return tlsf_realloc(tlsf_mem_pool, oldmem, bytes); + void *mem = tlsf_realloc(tlsf_mem_pool, oldmem, bytes); + if (!mem) + errno = ENOMEM; + + return mem; } EXPORT_SYMBOL(realloc); void *memalign(size_t alignment, size_t bytes) { - return tlsf_memalign(tlsf_mem_pool, alignment, bytes); + void *mem = tlsf_memalign(tlsf_mem_pool, alignment, bytes); + if (!mem) + errno = ENOMEM; + + return mem; } EXPORT_SYMBOL(memalign); |