Skip to content

Commit

Permalink
support auto add dram size to dtb
Browse files Browse the repository at this point in the history
  • Loading branch information
user authored and YuzukiTsuru committed Dec 12, 2023
1 parent 6458226 commit 1008cc5
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 49 deletions.
106 changes: 57 additions & 49 deletions app/syter_boot/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <common.h>
#include <jmp.h>
#include <smalloc.h>
#include <string.h>

#include "sys-dram.h"
#include "sys-sdcard.h"
Expand Down Expand Up @@ -327,73 +328,80 @@ static const char *find_entry_value(const IniEntry *entries, int entry_count, co
return NULL;
}

static int update_bootargs_from_config() {
static int update_bootargs_from_config(uint64_t dram_size) {
int ret = 0;
char *bootargs_str_config = NULL;

/* Check if using config file, get bootargs in the config file */
if (image.is_config) {
size_t size_a = strlen(image.config_dest);
int entry_count = parse_ini_data(image.config_dest, size_a, entries, MAX_ENTRY);

for (int i = 0; i < entry_count; ++i) {
/* Print parsed INI entries */
printk(LOG_LEVEL_DEBUG, "INI: [%s] %s = %s\n", entries[i].section, entries[i].key, entries[i].value);
}
bootargs_str_config = find_entry_value(entries, entry_count, "configs", "bootargs");
}

const char *bootargs_str_config = find_entry_value(entries, entry_count, "configs", "bootargs");
/* Force image.dest to be a pointer to fdt_header structure */
struct fdt_header *dtb_header = (struct fdt_header *) image.of_dest;

if (bootargs_str_config != NULL) {
/* Set bootargs based on the configuration file */
printk(LOG_LEVEL_DEBUG, "INI: Set bootargs to %s\n", bootargs_str_config);
/* Check if DTB header is valid */
if ((ret = fdt_check_header(dtb_header)) != 0) {
printk(LOG_LEVEL_ERROR, "Invalid device tree blob: %s\n", fdt_strerror(ret));
abort();
}

/* Force image.dest to be a pointer to fdt_header structure */
struct fdt_header *dtb_header = (struct fdt_header *) image.of_dest;
/* Get the total size of DTB */
uint32_t size = fdt_totalsize(image.of_dest);
printk(LOG_LEVEL_DEBUG, "%s: FDT Size = %d\n", image.of_filename, size);

/* Check if DTB header is valid */
if ((ret = fdt_check_header(dtb_header)) != 0) {
printk(LOG_LEVEL_ERROR, "Invalid device tree blob: %s\n", fdt_strerror(ret));
abort();
}
int len = 0;
/* Get the offset of "/chosen" node */
uint32_t bootargs_node = fdt_path_offset(image.of_dest, "/chosen");

/* Get the total size of DTB */
uint32_t size = fdt_totalsize(image.of_dest);
printk(LOG_LEVEL_DEBUG, "%s: FDT Size = %d\n", image.of_filename, size);

int len = 0;
/* Get the offset of "/chosen" node */
uint32_t bootargs_node = fdt_path_offset(image.of_dest, "/chosen");

/* Get bootargs string */
char *bootargs_str = (void *) fdt_getprop(image.of_dest, bootargs_node, "bootargs", &len);

_add_dts_size:
/* Modify bootargs string */
ret = fdt_setprop(image.of_dest, bootargs_node, "bootargs", bootargs_str_config, strlen(bootargs_str_config) + 1);
if (ret == -FDT_ERR_NOSPACE) {
printk(LOG_LEVEL_DEBUG, "FDT: FDT_ERR_NOSPACE, Size = %d, Increase Size = %d\n", size, 512);
ret = fdt_increase_size(image.of_dest, 512);
if (!ret)
goto _add_dts_size;
else
goto _err_size;
} else if (ret < 0) {
printk(LOG_LEVEL_ERROR, "Can't change bootargs node: %s\n", fdt_strerror(ret));
abort();
}
/* Get bootargs string */
char *bootargs_str = (void *) fdt_getprop(image.of_dest, bootargs_node, "bootargs", &len);

/* Get the total size of DTB */
printk(LOG_LEVEL_DEBUG, "Modify FDT Size = %d\n", fdt_totalsize(image.of_dest));
/* If config file read fail or not using */
if (bootargs_str_config == NULL) {
printk(LOG_LEVEL_WARNING, "INI: Cannot parse bootargs, using default bootargs in DTB.\n");
bootargs_str_config = bootargs_str;
}

if (ret < 0) {
printk(LOG_LEVEL_ERROR, "libfdt fdt_setprop() error: %s\n", fdt_strerror(ret));
abort();
}
} else {
printk(LOG_LEVEL_WARNING, "INI: Cannot parse bootargs, using default bootargs in DTB.\n");
}
/* Add dram size to dtb */
char dram_size_str[8];
strcat(bootargs_str_config, " mem=");
strcat(bootargs_str_config, simple_ltoa(dram_size, dram_size_str, 10));
strcat(bootargs_str_config, "M");

/* Set bootargs based on the configuration file */
printk(LOG_LEVEL_DEBUG, "INI: Set bootargs to %s\n", bootargs_str_config);

_add_dts_size:
/* Modify bootargs string */
ret = fdt_setprop(image.of_dest, bootargs_node, "bootargs", bootargs_str_config, strlen(bootargs_str_config) + 1);
if (ret == -FDT_ERR_NOSPACE) {
printk(LOG_LEVEL_DEBUG, "FDT: FDT_ERR_NOSPACE, Size = %d, Increase Size = %d\n", size, 512);
ret = fdt_increase_size(image.of_dest, 512);
if (!ret)
goto _add_dts_size;
else
goto _err_size;
} else if (ret < 0) {
printk(LOG_LEVEL_ERROR, "Can't change bootargs node: %s\n", fdt_strerror(ret));
abort();
}

return 0;
/* Get the total size of DTB */
printk(LOG_LEVEL_DEBUG, "Modify FDT Size = %d\n", fdt_totalsize(image.of_dest));

if (ret < 0) {
printk(LOG_LEVEL_ERROR, "libfdt fdt_setprop() error: %s\n", fdt_strerror(ret));
abort();
}

return 0;
_err_size:
printk(LOG_LEVEL_ERROR, "DTB: Can't increase blob size: %s\n", fdt_strerror(ret));
abort();
Expand Down Expand Up @@ -469,7 +477,7 @@ int main(void) {
}

/* Update boot arguments based on configuration file. */
update_bootargs_from_config();
update_bootargs_from_config(dram_size);

/* Set up boot parameters for the kernel. */
if (boot_image_setup((uint8_t *) image.dest, &entry_point)) {
Expand Down
2 changes: 2 additions & 0 deletions include/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,6 @@ extern unsigned long simple_strtoul(const char *cp, char **endp, unsigned int ba

extern unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base);

extern char* simple_ltoa(long int num, char* str, int base);

#endif /* #ifndef __STRING_H__ */
31 changes: 31 additions & 0 deletions src/string/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,35 @@ unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) {
*endp = (char *) cp;

return result;
}

char *simple_ltoa(long int num, char *str, int base) {
int i = 0;
int is_negative = 0;

if (num < 0 && base == 10) {
is_negative = 1;
num = -num;
}

do {
int digit = num % base;
str[i++] = (digit < 10) ? (digit + '0') : (digit - 10 + 'a');
num /= base;
} while (num > 0);

if (is_negative) {
str[i++] = '-';
}

str[i] = '\0';

int len = strlen(str);
for (int j = 0; j < len / 2; j++) {
char temp = str[j];
str[j] = str[len - j - 1];
str[len - j - 1] = temp;
}

return str;
}

0 comments on commit 1008cc5

Please sign in to comment.