in AZ3166/tools/dice_device_enrollment/src/dice_device_enrollment/dice_device_enrollment.cpp [453:551]
int main(int argc, char * argv[])
{
binFileFullPath[0] = 0;
mapFileFullPath[0] = 0;
registrationId = NULL;
udsString = NULL;
int result = parseCommandArguments(argc, argv);
if (result == -1)
{
printHelp();
return exitFunction(1);
}
else if (result == 1)
{
// Enter interact mode
fileName = get_user_input("Input the name of your project, default name is \"DevKitDPS\" :", fileNameLength);
if (strlen(fileName) == 0)
{
strcpy(fileName, "DevKitDPS");
}
// Check sanity of input files - .bin and .map
if (validateInputFiles() != 0)
{
return exitFunction(1);
}
// Get user input
udsString = get_user_input("Input the UDS you saved into security chip of your DevKit: ", 64);
// Registration ID
registrationId = get_user_input("Input your preferred registrationId as you input in DPS.ino(Click Enter to skip if no registrationId provided in DPS.ino): ", 128);
if (strlen(registrationId) == 0)
{
macAddress = get_user_input("Input the Mac Address on your DevKit: ", 12);
if (macAddressValidated() != 0)
{
return exitFunction(1);
}
firmwareVer = get_user_input("Input the firmware version of the program running on your DevKit: ", 5);
if (firmwareVerValidated() != 0)
{
return exitFunction(1);
}
// Auto Generate registrationId based on firmware version and device MAC address
autoGenRegistrationId();
}
}
// Validate the UDS
if (udsStringValidated() != 0)
{
return exitFunction(1);
}
else
{
// Prepare UDS from udsString
getUDSBytesFromString();
}
// Validate the Registration ID
if (registrationIdValidated())
{
return exitFunction(1);
}
// Get start address of flash from .bin file
unsigned long int resultAddress;
resultAddress = findAddressInMapFile(flashStartname);
if (resultAddress == 0)
{
return exitFunction(1);
}
startBin = (uint8_t*)resultAddress;
uint32_t riotSize;
uint32_t riotFwSize;
uint8_t* riotCore = getBinFileWithName(startRiotCoreName, stopRiotCoreName, riotSize);
uint8_t* riotFw = getBinFileWithName(startRiotFwName, stopRiotFwName, riotFwSize);
// Retrieve alias certificate
char* aliasCertBuffer = (char*)malloc(aliasCertSize);
memset(aliasCertBuffer, 0, aliasCertSize);
DiceRIoTStart(registrationId, riotCore, riotSize, riotFw, riotFwSize, aliasCertBuffer, aliasCertSize);
// Write the cert to pem file
FILE * opening;
char certFileName[64] = { '\0' };
sprintf(certFileName, "%s.pem", registrationId);
opening = fopen(certFileName, "w");
fprintf(opening, aliasCertBuffer);
fclose(opening);
printf("Writing to the Alias Key Certificate file %s is successful.\n", certFileName);
// Release allocations in heap
free(riotCore);
free(riotFw);
free(aliasCertBuffer);
return exitFunction(0);
}