Skip to content

Commit e50e46e

Browse files
authored
Fix parsing HEX file (#200)
***NO_CI***
1 parent 8b67385 commit e50e46e

File tree

1 file changed

+61
-76
lines changed

1 file changed

+61
-76
lines changed

nanoFirmwareFlasher.Library/FirmwarePackage.cs

Lines changed: 61 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -677,114 +677,99 @@ internal record struct DownloadUrlResult(string Url, string Version, ExitCodes O
677677
{
678678
}
679679

680-
private void FindBooterStartAddress()
680+
private uint FindStartAddressInHexFile(string hexFilePath)
681681
{
682-
uint address;
682+
uint address = 0;
683683

684-
if (string.IsNullOrEmpty(NanoClrFile))
685-
{
686-
// nothing to do here
687-
return;
688-
}
689-
690-
// find out what's the booter block start
684+
// find out what's the block start
691685

692686
// do this by reading the HEX format file...
693-
var textLines = File.ReadAllLines(NanoBooterFile);
687+
var textLines = File.ReadAllLines(hexFilePath);
694688

695689
// ... and decoding the start address
696690
var addressRecord = textLines.FirstOrDefault();
691+
string startAddress = string.Empty;
697692

698-
// 1st line is an Extended Segment Address Records (HEX86)
693+
// 1st line can be either:
694+
// 1) an Extended Segment Address Records (HEX86)
699695
// format ":02000004FFFFFC"
696+
// 2) a plain Data Record (HEX86)
697+
// format ":10246200464C5549442050524F46494C4500464C33"
700698

701-
// perform sanity checks
702-
if (addressRecord == null ||
703-
addressRecord.Length != 15 ||
704-
addressRecord.Substring(0, 9) != ":02000004")
699+
// perform sanity checks and...
700+
// ... check for Extended Segment Address Record
701+
if (addressRecord != null &&
702+
addressRecord.Length == 15 &&
703+
addressRecord.Substring(0, 9) == ":02000004")
705704
{
706-
// wrong format
707-
throw new FormatException("Wrong data in nanoBooter file");
708-
}
705+
startAddress = addressRecord.Substring(9, 4);
709706

710-
// looking good, grab the upper 16bits
711-
address = (uint)int.Parse(addressRecord.Substring(9, 4), System.Globalization.NumberStyles.HexNumber);
712-
address <<= 16;
707+
// looking good, grab the upper 16bits
708+
address = (uint)int.Parse(startAddress, System.Globalization.NumberStyles.HexNumber);
709+
address <<= 16;
713710

714-
// now the 2nd line to get the lower 16 bits of the address
715-
addressRecord = textLines.Skip(1).FirstOrDefault();
711+
// now the 2nd line to get the lower 16 bits of the address
712+
addressRecord = textLines.Skip(1).FirstOrDefault();
716713

717-
// 2nd line is a Data Record
718-
// format ":10246200464C5549442050524F46494C4500464C33"
714+
// 2nd line is a Data Record
715+
// format ":10246200464C5549442050524F46494C4500464C33"
719716

720-
// perform sanity checks
721-
if (addressRecord == null ||
722-
addressRecord.Substring(0, 1) != ":" ||
723-
addressRecord.Length < 7)
717+
// perform sanity checks
718+
if (addressRecord == null ||
719+
addressRecord.Substring(0, 1) != ":" ||
720+
addressRecord.Length < 7)
721+
{
722+
// wrong format
723+
throw new FormatException("Wrong data in nanoBooter file");
724+
}
725+
726+
// looking good, grab the lower 16bits
727+
address += (uint)int.Parse(addressRecord.Substring(3, 4), System.Globalization.NumberStyles.HexNumber);
728+
}
729+
// try now with Data Record format
730+
else if (addressRecord != null &&
731+
addressRecord.Length == 43 &&
732+
addressRecord.Substring(0, 3) == ":10")
733+
{
734+
startAddress = addressRecord.Substring(3, 4);
735+
736+
// looking good, grab the address
737+
address = (uint)int.Parse(startAddress, System.Globalization.NumberStyles.HexNumber);
738+
}
739+
740+
// do we have a valid one?
741+
if (string.IsNullOrEmpty(startAddress))
724742
{
725743
// wrong format
726744
throw new FormatException("Wrong data in nanoBooter file");
727745
}
728746

729-
// looking good, grab the lower 16bits
730-
address += (uint)int.Parse(addressRecord.Substring(3, 4), System.Globalization.NumberStyles.HexNumber);
731-
732-
BooterStartAddress = address;
747+
// all good
748+
return address;
733749
}
734750

735-
private void FindClrStartAddress()
751+
private void FindBooterStartAddress()
736752
{
737-
uint address;
738-
739-
if (string.IsNullOrEmpty(NanoClrFile))
753+
if (string.IsNullOrEmpty(NanoBooterFile))
740754
{
741755
// nothing to do here
742756
return;
743757
}
744758

745-
// find out what's the CLR block start
746-
747-
// do this by reading the HEX format file...
748-
var textLines = File.ReadAllLines(NanoClrFile);
749-
750-
// ... and decoding the start address
751-
var addressRecord = textLines.FirstOrDefault();
752-
753-
// 1st line is an Extended Segment Address Records (HEX86)
754-
// format ":02000004FFFFFC"
755-
756-
// perform sanity checks
757-
if (addressRecord == null ||
758-
addressRecord.Length != 15 ||
759-
addressRecord.Substring(0, 9) != ":02000004")
760-
{
761-
// wrong format
762-
throw new FormatException("Wrong data in nanoClr file");
763-
}
764-
765-
// looking good, grab the upper 16bits
766-
address = (uint)int.Parse(addressRecord.Substring(9, 4), System.Globalization.NumberStyles.HexNumber);
767-
address <<= 16;
768-
769-
// now the 2nd line to get the lower 16 bits of the address
770-
addressRecord = textLines.Skip(1).FirstOrDefault();
771-
772-
// 2nd line is a Data Record
773-
// format ":10246200464C5549442050524F46494C4500464C33"
759+
// find out what's the booter block start
760+
BooterStartAddress = FindStartAddressInHexFile(NanoBooterFile);
761+
}
774762

775-
// perform sanity checks
776-
if (addressRecord == null ||
777-
addressRecord.Substring(0, 1) != ":" ||
778-
addressRecord.Length < 7)
763+
private void FindClrStartAddress()
764+
{
765+
if (string.IsNullOrEmpty(NanoClrFile))
779766
{
780-
// wrong format
781-
throw new FormatException("Wrong data in nanoClr file");
767+
// nothing to do here
768+
return;
782769
}
783770

784-
// looking good, grab the lower 16bits
785-
address += (uint)int.Parse(addressRecord.Substring(3, 4), System.Globalization.NumberStyles.HexNumber);
786-
787-
ClrStartAddress = address;
771+
// find out what's the CLR block start
772+
ClrStartAddress = FindStartAddressInHexFile(NanoClrFile);
788773
}
789774

790775
internal void PostProcessDownloadAndExtract()

0 commit comments

Comments
 (0)