Skip to content

Commit 2a111d9

Browse files
committed
updated, but not working completely.
1 parent 2c5e691 commit 2a111d9

File tree

1 file changed

+79
-67
lines changed
  • secure_boot_and_secure_firmware_upgrade_over_canfd/icsp_inhibit.X

1 file changed

+79
-67
lines changed

secure_boot_and_secure_firmware_upgrade_over_canfd/icsp_inhibit.X/main.c

Lines changed: 79 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -29,56 +29,115 @@
2929
#include "mcc_generated_files/flash/flash_types.h"
3030
#include "mcc_generated_files/boot/boot_config.h"
3131

32-
#define WINDOW_SIZE 10
32+
#define USER_INPUT_BUFFER_SIZE 50
3333
#define UNLOCK_COMMAND "LOCKDEVICE"
34+
#define MATCHES 0
35+
#define ENTER '\r'
3436

3537
// Function prototypes
3638
static void ClearTerminalScreen(void);
3739
static void ClearTerminalLine(void);
3840
static void MoveCursor(int row);
3941
static void HideCursor(void);
4042
static void PrintWarning(void);
41-
static void AppendCharToWindow(char receivedChar, char *window, int *windowIndex);
42-
static void ProcessReceivedChar(char receivedChar, char *window, int *windowIndex);
43-
static void ResetWindowOnMismatch(char *window, int *windowIndex);
44-
static void CheckForUnlockCommand(char *window, int *windowIndex);
4543
static uint32_t GetResetAddress();
4644
static bool WasLoadedByBootloader();
4745
static void PrintBootloaderRequired(void);
46+
static char* ScanInput(void);
47+
static void DisableProgrammingPort(void);
48+
static void InvalidKeyword(void);
49+
static char userInput[USER_INPUT_BUFFER_SIZE] = {0};
50+
static bool ICSP_INHIBIT_IsEnabled(void); //temp stub - replace with real version
4851

4952
int main(void)
5053
{
51-
char window[WINDOW_SIZE + 1] = {0};
52-
int windowIndex = 0;
53-
5454
SYSTEM_Initialize();
5555
HideCursor();
5656
ClearTerminalScreen();
5757

58-
if(WasLoadedByBootloader() == false)
58+
// if(WasLoadedByBootloader() == false)
59+
// {
60+
// PrintBootloaderRequired();
61+
//
62+
// while(1)
63+
// {
64+
// }
65+
// }
66+
// else
5967
{
60-
PrintBootloaderRequired();
68+
const char* keyword = "LOCKDEVICE";
6169

62-
while(1)
63-
{
64-
}
65-
}
66-
else
67-
{
6870
PrintWarning();
6971

7072
while (1)
7173
{
72-
if (UART1_IsRxReady())
74+
char* userInput;
75+
76+
if(ICSP_INHIBIT_IsEnabled())
77+
{
78+
printf("ICSP Programming/Debugging permanently disabled.");
79+
80+
while(1)
81+
{
82+
}
83+
}
84+
85+
userInput = ScanInput();
86+
87+
if(strcmp(userInput, keyword) == MATCHES)
7388
{
74-
char receivedChar = UART1_Read();
75-
ProcessReceivedChar(receivedChar, window, &windowIndex);
76-
CheckForUnlockCommand(window, &windowIndex);
89+
DisableProgrammingPort();
90+
}
91+
else
92+
{
93+
InvalidKeyword();
7794
}
7895
}
7996
}
8097
}
8198

99+
//temp stub - replace with real version
100+
static bool ICSP_INHIBIT_IsEnabled(void)
101+
{
102+
return false;
103+
}
104+
105+
static void InvalidKeyword(void)
106+
{
107+
MoveCursor(10);
108+
ClearTerminalLine();
109+
MoveCursor(5);
110+
ClearTerminalLine();
111+
printf("Invalid character entered. Try again.");
112+
}
113+
114+
static void DisableProgrammingPort(void)
115+
{
116+
//ICSP_INHIBIT_Enable(true);
117+
}
118+
119+
static char* ScanInput(void)
120+
{
121+
uint8_t offset = 0;
122+
char key;
123+
124+
memset(userInput, 0, sizeof(userInput));
125+
126+
do
127+
{
128+
key = UART1_Read();
129+
130+
if(key != ENTER)
131+
{
132+
userInput[offset++] = key;
133+
printf("%c", key);
134+
}
135+
}
136+
while((key != ENTER) && (offset < sizeof(userInput)));
137+
138+
return userInput;
139+
}
140+
82141
static void ClearTerminalScreen(void)
83142
{
84143
printf("\033[2J");
@@ -113,53 +172,6 @@ static void PrintBootloaderRequired(void)
113172
printf("Because programming will be permanently disabled, \r\na bootloader is required to run this demo. \r\nPlease see the readme.md for more information.");
114173
}
115174

116-
static void ProcessReceivedChar(char receivedChar, char *window, int *windowIndex)
117-
{
118-
bool isCharValid = (*windowIndex < strlen(UNLOCK_COMMAND)) && (receivedChar == UNLOCK_COMMAND[*windowIndex]);
119-
120-
if (isCharValid)
121-
{
122-
AppendCharToWindow(receivedChar, window, windowIndex);
123-
MoveCursor(10);
124-
printf("%s", window);
125-
}
126-
else
127-
{
128-
ResetWindowOnMismatch(window, windowIndex);
129-
}
130-
}
131-
132-
static void AppendCharToWindow(char receivedChar, char *window, int *windowIndex)
133-
{
134-
window[(*windowIndex)++] = receivedChar;
135-
window[*windowIndex] = '\0';
136-
}
137-
138-
static void ResetWindowOnMismatch(char *window, int *windowIndex)
139-
{
140-
MoveCursor(10);
141-
ClearTerminalLine();
142-
MoveCursor(5);
143-
ClearTerminalLine();
144-
printf("Invalid character entered. Try again.");
145-
*windowIndex = 0;
146-
memset(window, 0, WINDOW_SIZE + 1);
147-
}
148-
149-
static void CheckForUnlockCommand(char *window, int *windowIndex)
150-
{
151-
if (strncmp(window, UNLOCK_COMMAND, *windowIndex) == 0 && *windowIndex == strlen(UNLOCK_COMMAND))
152-
{
153-
MoveCursor(5);
154-
ClearTerminalLine();
155-
printf("ICSP Programming/Debugging permanently disabled. \n");
156-
MoveCursor(10);
157-
ClearTerminalLine();
158-
*windowIndex = 0;
159-
memset(window, 0, WINDOW_SIZE + 1);
160-
}
161-
}
162-
163175
/* The following code finds the address used by GOTO instruction programmed
164176
* at the reset vector in order to determine whether or not the application
165177
* was downloaded directly or via the bootloader (i.e. if the address within the

0 commit comments

Comments
 (0)