Skip to content

Commit abe2a1d

Browse files
authored
Merge pull request #357 from BenReed161/add-image-redundancy-subcmd
Add redundant image partition code
2 parents 89626a6 + b034946 commit abe2a1d

File tree

4 files changed

+157
-3
lines changed

4 files changed

+157
-3
lines changed

cli/main.c

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1906,11 +1906,12 @@ static void print_fw_part_line(const char *tag,
19061906
if (!inf)
19071907
return;
19081908

1909-
printf(" %-4s\tVersion: %-8s\tCRC: %08lx\t%4s%11s%s\n",
1909+
printf(" %-4s\tVersion: %-8s\tCRC: %08lx\t%4s%11s%s%s\n",
19101910
tag, inf->version, inf->image_crc,
19111911
inf->read_only ? "(RO)" : "",
19121912
inf->running ? " (Running)" : "",
1913-
inf->valid ? "" : " (Invalid)");
1913+
inf->valid ? "" : " (Invalid)",
1914+
inf->redundant ? " (Redundant)" : "");
19141915
}
19151916

19161917
static int print_fw_part_info(struct switchtec_dev *dev)
@@ -2177,7 +2178,7 @@ static int fw_toggle(int argc, char **argv)
21772178
"are not supported by Gen3 switches\n");
21782179
return 1;
21792180
} else if (cfg.riotcore && switchtec_is_gen4(cfg.dev)){
2180-
fprintf(stderr, "Firmware type RIOTCORE is not supported by Gen4 switchtes\n");
2181+
fprintf(stderr, "Firmware type RIOTCORE is not supported by Gen4 switches\n");
21812182
return 1;
21822183
} else {
21832184
ret = switchtec_fw_toggle_active_partition(cfg.dev,
@@ -2205,6 +2206,69 @@ static int fw_toggle(int argc, char **argv)
22052206
return ret;
22062207
}
22072208

2209+
#define CMD_DESC_FW_REDUNDANT "Set an image partition to redundant"
2210+
static int fw_redundant(int argc, char **argv)
2211+
{
2212+
2213+
int ret = 0;
2214+
2215+
static struct {
2216+
struct switchtec_dev *dev;
2217+
int bl2;
2218+
int key;
2219+
int firmware;
2220+
int config;
2221+
int riotcore;
2222+
int redundant;
2223+
} cfg = {
2224+
.redundant = 1,
2225+
};
2226+
const struct argconfig_options opts[] = {
2227+
DEVICE_OPTION,
2228+
{"bl2", 'b', "", CFG_NONE, &cfg.bl2, no_argument,
2229+
"Set redundancy flag for bl2 partition"},
2230+
{"key", 'k', "", CFG_NONE, &cfg.key, no_argument,
2231+
"Set redundancy flag for Key manifest partition"},
2232+
{"firmware", 'f', "", CFG_NONE, &cfg.firmware, no_argument,
2233+
"Set redundancy flag for main FW image partition"},
2234+
{"config", 'c', "", CFG_NONE, &cfg.config, no_argument,
2235+
"Set redundancy flag for data CFG partition"},
2236+
{"riotcore", 'r', "", CFG_NONE, &cfg.riotcore, no_argument,
2237+
"Set redundancy flag for RIOTCORE partition"},
2238+
{"redundant", 'R', "unset - 0 / set - 1", CFG_INT, &cfg.redundant, required_argument,
2239+
"Set the redundant flag for the selected image type(s). If left blank will set (1)"},
2240+
{NULL}};
2241+
2242+
argconfig_parse(argc, argv, CMD_DESC_FW_REDUNDANT, opts, &cfg, sizeof(cfg));
2243+
2244+
if (!switchtec_is_gen5(cfg.dev)) {
2245+
fprintf(stderr, "Setting the redundant flag is only supported on Gen5 switches\n");
2246+
return 1;
2247+
}
2248+
if (!cfg.bl2 && !cfg.key && !cfg.firmware && !cfg.config && !cfg.riotcore) {
2249+
fprintf(stderr, "Not setting image partition(s) as redundant since no partition type was specified\n");
2250+
return 1;
2251+
}
2252+
if (cfg.redundant > 1) {
2253+
fprintf(stderr, "Set redundant flag to either set - 1 or unset - 0\n");
2254+
return 1;
2255+
}
2256+
2257+
ret = switchtec_fw_set_redundant_flag(cfg.dev, cfg.key,
2258+
cfg.riotcore, cfg.bl2,
2259+
cfg.config, cfg.firmware,
2260+
cfg.redundant);
2261+
if (ret)
2262+
switchtec_perror("set redundant flag");
2263+
2264+
ret = print_fw_part_info(cfg.dev);
2265+
if (ret)
2266+
switchtec_perror("print fw info");
2267+
2268+
printf("\n");
2269+
return ret;
2270+
}
2271+
22082272
#define CMD_DESC_FW_READ "read a firmware image from flash"
22092273

22102274
static int fw_read(int argc, char **argv)
@@ -2781,6 +2845,7 @@ static const struct cmd commands[] = {
27812845
CMD(fw_update, CMD_DESC_FW_UPDATE),
27822846
CMD(fw_info, CMD_DESC_FW_INFO),
27832847
CMD(fw_toggle, CMD_DESC_FW_TOGGLE),
2848+
CMD(fw_redundant, CMD_DESC_FW_REDUNDANT),
27842849
CMD(fw_read, CMD_DESC_FW_READ),
27852850
CMD(fw_img_info, CMD_DESC_FW_IMG_INFO),
27862851
CMD(evcntr, CMD_DESC_EVCNTR),

inc/switchtec/mrpc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ enum mrpc_sub_cmd {
143143
MRPC_FWDNLD_DOWNLOAD = 1,
144144
MRPC_FWDNLD_TOGGLE = 2,
145145
MRPC_FWDNLD_BOOT_RO = 4,
146+
MRPC_FWDNLD_SET_RDNDNT = 5,
146147

147148
MRPC_VGPIO_GET_DIR = 0,
148149
MRPC_VGPIO_GET_POL = 1,

inc/switchtec/switchtec.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,17 @@ enum switchtec_boot_phase {
115115
SWITCHTEC_BOOT_PHASE_UNKNOWN
116116
};
117117

118+
/**
119+
* @brief Device parition types GEN5
120+
*/
121+
enum switchtec_parition_types {
122+
SWITCHTEC_PART_TYPE_KEYMAN = 1,
123+
SWITCHTEC_PART_TYPE_RC = 2,
124+
SWITCHTEC_PART_TYPE_BL2 = 3,
125+
SWITCHTEC_PART_TYPE_CFG = 4,
126+
SWITCHTEC_PART_TYPE_FW = 5
127+
};
128+
118129
/**
119130
* @brief The variant types of Switchtec device
120131
*/
@@ -277,6 +288,7 @@ struct switchtec_fw_image_info {
277288

278289
unsigned long secure_version;
279290
bool signed_image;
291+
uint8_t redundant;
280292
};
281293

282294
struct switchtec_fw_part_summary {
@@ -971,6 +983,10 @@ enum switchtec_fw_ro {
971983
SWITCHTEC_FW_RO = 1,
972984
};
973985

986+
int switchtec_fw_set_redundant_flag(struct switchtec_dev *dev,
987+
int keyman, int riot,
988+
int bl2, int cfg, int fw,
989+
int set);
974990
int switchtec_fw_toggle_active_partition(struct switchtec_dev *dev,
975991
int toggle_bl2, int toggle_key,
976992
int toggle_fw, int toggle_cfg,

lib/fw.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,68 @@ static int switchtec_fw_wait(struct switchtec_dev *dev,
235235
return 0;
236236
}
237237

238+
static int set_redundant(struct switchtec_dev *dev, int type, int set)
239+
{
240+
int ret;
241+
char *part_types[] = {
242+
"KEYMAN",
243+
"RIOT",
244+
"BL2",
245+
"CFG",
246+
"MAIN-FW"
247+
};
248+
249+
struct {
250+
uint8_t subcmd;
251+
uint8_t part_type;
252+
uint8_t redundant_val;
253+
uint8_t reserved;
254+
} cmd;
255+
256+
cmd.subcmd = MRPC_FWDNLD_SET_RDNDNT;
257+
cmd.redundant_val = set;
258+
cmd.part_type = type;
259+
260+
printf("%s redundant flag \t(%s)\n", set ? "Checking" : "Un-checking",
261+
part_types[type-1]);
262+
ret = switchtec_cmd(dev, MRPC_FWDNLD, &cmd, sizeof(cmd), NULL, 0);
263+
if (ret) {
264+
fprintf(stderr, "Error: setting redudant flag \t(%s)\n",
265+
part_types[type-1]);
266+
return 1;
267+
}
268+
else {
269+
printf("Success: set redundant flag \t(%s)\n",
270+
part_types[type-1]);
271+
}
272+
return 0;
273+
}
274+
275+
/**
276+
* @brief Set the redundant image flag for the specified image types
277+
* @param[in] dev Switchtec device handle
278+
* @param[in] toggle_arr Pointer to the array of togglable image types
279+
* @return 0 on success, error code on failure
280+
*/
281+
int switchtec_fw_set_redundant_flag (struct switchtec_dev *dev, int keyman,
282+
int riot, int bl2, int cfg, int fw,
283+
int set)
284+
{
285+
int ret = 0;
286+
if(keyman)
287+
ret += set_redundant(dev, SWITCHTEC_PART_TYPE_KEYMAN, set);
288+
if (riot)
289+
ret += set_redundant(dev, SWITCHTEC_PART_TYPE_RC, set);
290+
if (bl2)
291+
ret += set_redundant(dev, SWITCHTEC_PART_TYPE_BL2, set);
292+
if (cfg)
293+
ret += set_redundant(dev, SWITCHTEC_PART_TYPE_CFG, set);
294+
if (fw)
295+
ret += set_redundant(dev, SWITCHTEC_PART_TYPE_FW, set);
296+
297+
return ret;
298+
}
299+
238300
/**
239301
* @brief Toggle the active firmware partition for the main or configuration
240302
* images.
@@ -1252,33 +1314,43 @@ static int switchtec_fw_part_info_gen5(struct switchtec_dev *dev,
12521314
part_info = &all->map1;
12531315
break;
12541316
case SWITCHTEC_FW_PART_ID_G5_RIOT0:
1317+
inf->redundant = all->riot_redundant_flag;
12551318
part_info = &all->riot0;
12561319
break;
12571320
case SWITCHTEC_FW_PART_ID_G5_RIOT1:
1321+
inf->redundant = all->riot_redundant_flag;
12581322
part_info = &all->riot1;
12591323
break;
12601324
case SWITCHTEC_FW_PART_ID_G5_KEY0:
1325+
inf->redundant = all->key_redundant_flag;
12611326
part_info = &all->keyman0;
12621327
break;
12631328
case SWITCHTEC_FW_PART_ID_G5_KEY1:
1329+
inf->redundant = all->key_redundant_flag;
12641330
part_info = &all->keyman1;
12651331
break;
12661332
case SWITCHTEC_FW_PART_ID_G5_BL20:
1333+
inf->redundant = all->bl2_redundant_flag;
12671334
part_info = &all->bl20;
12681335
break;
12691336
case SWITCHTEC_FW_PART_ID_G5_BL21:
1337+
inf->redundant = all->bl2_redundant_flag;
12701338
part_info = &all->bl21;
12711339
break;
12721340
case SWITCHTEC_FW_PART_ID_G5_IMG0:
1341+
inf->redundant = all->img_redundant_flag;
12731342
part_info = &all->img0;
12741343
break;
12751344
case SWITCHTEC_FW_PART_ID_G5_IMG1:
1345+
inf->redundant = all->img_redundant_flag;
12761346
part_info = &all->img1;
12771347
break;
12781348
case SWITCHTEC_FW_PART_ID_G5_CFG0:
1349+
inf->redundant = all->cfg_redundant_flag;
12791350
part_info = &all->cfg0;
12801351
break;
12811352
case SWITCHTEC_FW_PART_ID_G5_CFG1:
1353+
inf->redundant = all->cfg_redundant_flag;
12821354
part_info = &all->cfg1;
12831355
break;
12841356
case SWITCHTEC_FW_PART_ID_G5_NVLOG:

0 commit comments

Comments
 (0)