Skip to content

Commit 88645ee

Browse files
committed
Introduce keygen --no-overwrite to avoid prompt
1 parent c02c273 commit 88645ee

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

tools/keytools/keygen.c

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
/* Globals */
113113
static FILE *fpub, *fpub_image;
114114
static int force = 0;
115+
static int no_overwrite = 0; /* when set, avoids prompt if !force and files exist */
115116
#if defined(WOLFBOOT_RENESAS_RSIP) || \
116117
defined(WOLFBOOT_RENESAS_TSIP) || \
117118
defined(WOLFBOOT_RENESAS_SCEPROTECT)
@@ -1155,18 +1156,24 @@ static void key_gen_check(const char *kfilename)
11551156
FILE *f;
11561157
f = fopen(kfilename, "rb");
11571158
if (!force && (f != NULL)) {
1158-
char reply[40];
1159-
int replySz;
1160-
printf("** Warning: key file already exists! Are you sure you want to generate a new key and overwrite the existing key? [Type 'Yes']: ");
1161-
fflush(stdout);
1162-
replySz = scanf("%s", reply);
1163-
printf("Reply is [%s]\n", reply);
1164-
fclose(f);
1165-
if (replySz < 0 || strcmp(reply, "Yes") != 0) {
1166-
printf("Operation aborted by user.");
1167-
exit(5);
1168-
} else {
1169-
unlink(kfilename);
1159+
if (no_overwrite) {
1160+
printf("** Warning: key file already exists and will not be overwritten!");
1161+
}
1162+
else {
1163+
char reply[40];
1164+
int replySz;
1165+
printf("** Warning: key file already exists! Are you sure you want to generate a new key and overwrite the existing key? [Type 'Yes']: ");
1166+
fflush(stdout);
1167+
replySz = scanf("%s", reply);
1168+
printf("Reply is [%s]\n", reply);
1169+
fclose(f);
1170+
if (replySz < 0 || strcmp(reply, "Yes") != 0) {
1171+
printf("Operation aborted by user.");
1172+
exit(5);
1173+
}
1174+
else {
1175+
unlink(kfilename);
1176+
}
11701177
}
11711178
}
11721179
}
@@ -1402,6 +1409,9 @@ int main(int argc, char** argv)
14021409
else if (strcmp(argv[i], "--force") == 0) {
14031410
force = 1;
14041411
}
1412+
else if (strcmp(argv[i], "--no-overwrite") == 0) {
1413+
no_overwrite = 1;
1414+
}
14051415
else if (strcmp(argv[i], "--der") == 0) {
14061416
saveAsDer = 1;
14071417
}
@@ -1436,6 +1446,7 @@ int main(int argc, char** argv)
14361446
i++;
14371447
sprintf(pubkeyfile,"%s%s", argv[i], "/keystore.c");
14381448
sprintf(pubkeyimg, "%s%s", argv[i], "/keystore.der");
1449+
printf("keystore file: %s\n", pubkeyfile);
14391450
i++;
14401451
continue;
14411452
}
@@ -1458,15 +1469,20 @@ int main(int argc, char** argv)
14581469
exit(0);
14591470
fpub = fopen(pubkeyfile, "rb");
14601471
if (!force && (fpub != NULL)) {
1472+
if (no_overwrite) {
1473+
printf("** Not overwriting existing keystore file: %s\n", pubkeyfile);
1474+
exit(0);
1475+
}
14611476
char reply[40];
14621477
int replySz;
1463-
printf("** Warning: keystore already exists! Are you sure you want to generate a new key and overwrite the existing key? [Type 'Yes']: ");
1478+
printf("** Warning: keystore file already exists! %s\n", pubkeyfile);
1479+
printf("Are you sure you want to generate a new key and overwrite the existing key ? [Type 'Yes'] : ");
14641480
fflush(stdout);
14651481
replySz = scanf("%s", reply);
14661482
printf("Reply is [%s]\n", reply);
14671483
fclose(fpub);
14681484
if (replySz < 0 || strcmp(reply, "Yes") != 0) {
1469-
printf("Operation aborted by user.");
1485+
printf("Operation aborted by user.\n");
14701486
exit(5);
14711487
} else {
14721488
unlink(pubkeyfile);

0 commit comments

Comments
 (0)