summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZheng Bao <fishbaozi@gmail.com>2022-03-01 17:22:52 +0800
committerFelix Held <felix-coreboot@felixheld.de>2022-03-21 16:55:46 +0000
commit52a1898d4430e81f8249561724ab0e911ba900b8 (patch)
tree30ad442936daecc22c620e48f25357e78ae81b57
parent0c893d2624716f8fab95b4d60b0a22ab69ea74c8 (diff)
amdfwtool: Check the length of matching string before accessing
If AB recovery is enabled and get a "Lx" in fw.cfg, wrong character is got or access violation happens. Change-Id: Ibd8ffe34fd44d860ec2115cd36117da7b02169cd Signed-off-by: Zheng Bao <fishbaozi@gmail.com> Reviewed-on: https://review.coreboot.org/c/coreboot/+/62483 Tested-by: build bot (Jenkins) <no-reply@coreboot.org> Reviewed-by: Felix Held <felix-coreboot@felixheld.de> Reviewed-by: Jason Glenesk <jason.glenesk@gmail.com>
-rw-r--r--util/amdfwtool/data_parse.c31
1 files changed, 21 insertions, 10 deletions
diff --git a/util/amdfwtool/data_parse.c b/util/amdfwtool/data_parse.c
index 7f1bc3a1f3..e883d17d0e 100644
--- a/util/amdfwtool/data_parse.c
+++ b/util/amdfwtool/data_parse.c
@@ -472,6 +472,25 @@ static int skip_comment_blank_line(char *oneline)
return retval;
}
+char get_level_from_config(char *line, regoff_t level_index, amd_cb_config *cb_config)
+{
+ char lvl = 'x';
+ /* If the optional level field is present,
+ extract the level char. */
+ if (level_index != -1) {
+ if (cb_config->recovery_ab == 0)
+ lvl = line[level_index + 1];
+ else if (strlen(&line[level_index]) >= 3)
+ lvl = line[level_index + 2];
+ }
+
+ assert(lvl == 'x' || lvl == 'X' ||
+ lvl == 'b' || lvl == 'B' ||
+ lvl == '1' || lvl == '2');
+
+ return lvl;
+}
+
/*
return value:
0: The config file can not be parsed correctly.
@@ -537,16 +556,8 @@ uint8_t process_config(FILE *config, amd_cb_config *cb_config, uint8_t print_dep
/* If the optional level field is present,
extract the level char. */
- if (match[3].rm_so != -1) {
- if (cb_config->recovery_ab == 0)
- ch_lvl = oneline[match[3].rm_so + 1];
- else
- ch_lvl = oneline[match[3].rm_so + 2];
- }
-
- assert(ch_lvl == 'x' || ch_lvl == 'X' ||
- ch_lvl == 'b' || ch_lvl == 'B' ||
- ch_lvl == '1' || ch_lvl == '2');
+ ch_lvl = get_level_from_config(oneline,
+ match[3].rm_so, cb_config);
if (find_register_fw_filename_psp_dir(
&(oneline[match[1].rm_so]),