blob: 4cb4a7ad40ec007f573d465134db7f4677858384 [file] [log] [blame]
/*
* Copyright (c) 2020 The Fuchsia Authors
*/
#include <common.h>
#include <zircon/boot_args.h>
#include <stdlib.h>
static char *args = NULL;
static uint32_t args_len = 0;
// Chosen arbitrarily to make overflow checking easier so that we won't "wrap
// around" a uint32_t more than once. Can increase if needed.
#define MAX_ARG_LEN 2048
int zircon_store_cmdline(const char *payload)
{
if (!payload) {
return -1;
}
size_t length = strlen(payload);
if (length == 0) {
return -1;
}
if (length > MAX_ARG_LEN) {
printf("Error: arg is too big\n");
return -1;
}
uint32_t new_args_len;
if (args) {
// a ' ' is inserted after previous args
new_args_len = args_len + length + 1;
} else {
new_args_len = length;
}
// Make sure new_args_len didn't overflow.
if (new_args_len < args_len) {
printf("Error: can't fit this arg\n");
return -1;
}
char *new_args = malloc((size_t)new_args_len + 1);
if (!new_args) {
return -1;
}
new_args[new_args_len] = '\0';
if (args) {
memcpy(new_args, args, args_len);
free(args);
new_args[args_len] = ' ';
memcpy(new_args + args_len + 1, payload, length);
} else {
memcpy(new_args, payload, length);
}
args_len = new_args_len;
args = new_args;
printf("args: %s, args_len: %u\n", args, args_len);
return 0;
}
int zircon_clear_stored_cmdline()
{
if (args) {
free(args);
args = NULL;
}
args_len = 0;
return 0;
}
int zircon_append_cmdline(zbi_header_t *zbi, size_t capacity)
{
int ret = 0;
if (args) {
ret = append_zbi_item_or_log(zbi, capacity, ZBI_TYPE_CMDLINE, 0,
args, args_len + 1);
free(args);
args = NULL;
args_len = 0;
}
return ret;
}