Skip to content
Snippets Groups Projects
Commit b729a7e1 authored by Jonas Höppner's avatar Jonas Höppner
Browse files

[CMD][SECO_CONFIG] Fix accidental modification of environment variables

The strtok was directly called on the pointer returned from the uboot
env function, which is directly giving access to the cached data.
As strtok modifies the string it gets by inserting a \0 at the split,
followup reads of the same variable get a different result (only the
first element before the split.
To fix this, the string is now duplicated before spliting.
parent 682bbfea
No related branches found
No related tags found
1 merge request!224[CMD][SECO_CONFIG] Fix accidental modification of environment variables
......@@ -400,58 +400,67 @@ void selection_fdt_overlay (overlay_list_t list[], int num_element, int *selecti
char *create_fdt_overlay_load_string(data_boot_dev_t *table_fdt, data_boot_dev_t *table_overlay,
int n_element, char *file_list, int fdt_selected_device ) {
int n_element, const char *file_list, int fdt_selected_device ) {
int i = 0;
int sel_str_load = 0;
char *list;
char *fdt_load;
char *fdtoverlay2ram;
char *fdtoverlay2ram = NULL;
char *local_file_list = NULL;
/* strtok modifies the string by inserting a \0 at the split.
Create a copy to leave the original string as is. */
local_file_list = strdup(file_list);
if ( local_file_list == NULL ){
fprintf(stderr, "%s: Error: Failed to allocate memory.\n", __func__);
goto cleanup;
}
list = strtok( file_list, " " );
list = strtok( local_file_list, " " );
if ( list == NULL ) {
goto cleanup;
}
if ( list != NULL ) {
fdtoverlay2ram = malloc( sizeof(char) * 512 );
if ( fdtoverlay2ram == NULL )
return NULL;
fdtoverlay2ram = malloc( sizeof(char) * 512 );
if ( fdtoverlay2ram == NULL )
goto cleanup;
memset( fdtoverlay2ram, 0, sizeof( fdtoverlay2ram ) );
memset( fdtoverlay2ram, 0, sizeof( fdtoverlay2ram ) );
if ( fdt_selected_device == -1 ) {
fdt_load = GET_FDT_BOOT_LOAD;
if ( fdt_load != NULL ) {
for ( i = 0 ; i < n_element ; i++ ) {
if ( strcmp( &table_fdt[i].env_str[0], fdt_load ) == 0 ) {
sel_str_load = i;
break;
}
if ( fdt_selected_device == -1 ) {
fdt_load = GET_FDT_BOOT_LOAD;
if ( fdt_load != NULL ) {
for ( i = 0 ; i < n_element ; i++ ) {
if ( strcmp( &table_fdt[i].env_str[0], fdt_load ) == 0 ) {
sel_str_load = i;
break;
}
}
} else {
sel_str_load = fdt_selected_device;
}
}
while ( list != NULL ) {
sprintf( fdtoverlay2ram, "%s %s 0x%08X %s; %s 0x%08X;",
fdtoverlay2ram,
table_overlay[sel_str_load].env_str,
ENV_FDT_OVERLAY_BASEADDR + ENV_FDT_OVERLAY_BASEADDR_OFFSET * i,
list,
__stringify(MACRO_ENV_FDT_OVERLAY_APPLY),
ENV_FDT_OVERLAY_BASEADDR + ENV_FDT_OVERLAY_BASEADDR_OFFSET * i );
i++;
list = strtok( NULL, " " );
} else {
sel_str_load = fdt_selected_device;
}
}
while ( list != NULL ) {
} else {
sprintf( fdtoverlay2ram, "%s %s 0x%08X %s; %s 0x%08X;",
fdtoverlay2ram,
table_overlay[sel_str_load].env_str,
ENV_FDT_OVERLAY_BASEADDR + ENV_FDT_OVERLAY_BASEADDR_OFFSET * i,
list,
__stringify(MACRO_ENV_FDT_OVERLAY_APPLY),
ENV_FDT_OVERLAY_BASEADDR + ENV_FDT_OVERLAY_BASEADDR_OFFSET * i );
return NULL;
i++;
list = strtok( NULL, " " );
}
cleanup:
if(local_file_list)
free( local_file_list);
return fdtoverlay2ram;
}
#endif /* CONFIG_OF_LIBFDT_OVERLAY */
......@@ -1005,24 +1014,33 @@ char *create_fw_load_string(data_boot_dev_t *table_fdt, data_boot_dev_t *table_f
char *setting;
int size = 0;
char *fdt_load;
char *fw_load_cmd;
setting = strtok( file_list, ";" );
if ( setting != NULL ) {
while ( setting != NULL ) {
strcpy(&list[size][0], setting);
printf( "\nFW LOAD: new setting: %s\n", list[size] );
size++;
setting = strtok( NULL, ";" );
}
} else {
return NULL;
char *fw_load_cmd = NULL;
char *local_file_list = NULL;
/* strtok modifies the string by inserting a \0 at the split.
Create a copy to leave the original string as is. */
local_file_list = strdup(file_list);
if ( local_file_list == NULL ){
fprintf(stderr, "%s: Error: Failed to allocate memory.\n", __func__);
goto cleanup;
}
fw_load_cmd = malloc( MAX_FW_LOAD_CMD );
if ( fw_load_cmd == NULL )
return NULL;
setting = strtok( local_file_list, ";" );
if ( setting == NULL )
goto cleanup;
while ( setting != NULL ) {
strcpy(&list[size][0], setting);
printf( "\nFW LOAD: new setting: %s\n", list[size] );
size++;
setting = strtok( NULL, ";" );
}
fw_load_cmd = malloc( MAX_FW_LOAD_CMD );
if ( fw_load_cmd == NULL ){
fprintf(stderr, "%s: Error: Failed to allocate memory.\n", __func__);
goto cleanup;
}
memset( fw_load_cmd, 0, sizeof( fw_load_cmd ) );
if ( fdt_selected_device == -1 ) {
......@@ -1040,7 +1058,7 @@ char *create_fw_load_string(data_boot_dev_t *table_fdt, data_boot_dev_t *table_f
}
for(i=0; i<size; i++) {
char *tmp = &list[i][0];
char *file_name = strsep( &tmp, " " );
char *address = strsep( &tmp, " " );
......@@ -1052,8 +1070,11 @@ char *create_fw_load_string(data_boot_dev_t *table_fdt, data_boot_dev_t *table_f
strcat( fw_load_cmd, (const char *)new_cmd );
}
return fw_load_cmd;
cleanup:
if( local_file_list )
free(local_file_list);
return fw_load_cmd;
}
void clear_fw_list( void ) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment