Public Class Main #Region " Declarations " 'Error codes Private Const _WRONG_NUM_ARGUMENTS As Integer = 1 Private Const _UNKNOWN_EXCEPTION As Integer = 2 Private Const _INVALID_BOOT_SIZE As Integer = 3 Private Const _LAST_PAGE As Integer = &H1F Private Const _MIN_ARGUMENTS As Integer = 2 Private Const _MAX_ARGUMENTS As Integer = 3 Private Const _PAGE_SIZE As Integer = 16384 #End Region #Region " Public Methods " Public Shared Sub Main(ByVal args() As String) 'Validate correct number of arguments If args.Length < _MIN_ARGUMENTS OrElse args.Length > _MAX_ARGUMENTS Then _DisplayUsage(_WRONG_NUM_ARGUMENTS, "Invalid number of arguments!") Exit Sub End If Try 'Create dictionary of 16KB pages Dim pages As New Dictionary(Of Integer, Object) Dim page(_PAGE_SIZE - 1) As Byte Dim info As IO.FileInfo Dim imageFile As IO.FileStream Dim line As IntelHexLine Dim currentPage As Integer 'Read in boot page image If args.Length = _MAX_ARGUMENTS Then info = New IO.FileInfo(args(1)) If info.Length <> _PAGE_SIZE Then Throw New ConvertException(_INVALID_BOOT_SIZE, _ "Boot image is not 16KB!") End If page = IO.File.ReadAllBytes(args(1)) pages.Add(_LAST_PAGE, page.Clone()) End If 'Read in OS upgrade file imageFile = New IO.FileStream(args(0), IO.FileMode.Open, IO.FileAccess.Read) imageFile.Seek(&H4E, IO.SeekOrigin.Begin) 'Find the start of the page data Do line = _GetIntelHexLine(imageFile) Loop While line.Type <> IntelHexLine.LineType.StartEndMarker 'Pull in all of the page data Do line = _GetIntelHexLine(imageFile) Select Case line.Type Case IntelHexLine.LineType.PageStart currentPage = Convert.ToInt32(line.Data(1)) Case IntelHexLine.LineType.Data If Not pages.ContainsKey(currentPage) Then 'We don't have this page yet, so blank it out and add it first For i As Integer = 0 To _PAGE_SIZE - 1 page(i) = &HFF Next pages.Add(currentPage, page.Clone()) End If 'Put the data in the page For i As Integer = 0 To line.Data.Length - 1 DirectCast(pages(currentPage), Byte())((line.Address And &H3FFF) + i) = line.Data(i) Next End Select Loop While line.Type <> IntelHexLine.LineType.StartEndMarker imageFile.Close() 'Write pages out to ROM image imageFile = New IO.FileStream(args(args.Length - 1), IO.FileMode.Create) For i As Integer = 0 To _LAST_PAGE imageFile.Seek(i * _PAGE_SIZE, IO.SeekOrigin.Begin) If pages.ContainsKey(i) Then 'We have this page, write it to the file imageFile.Write(DirectCast(pages(i), Byte()), 0, _PAGE_SIZE) Else 'We don't have this page, fill it with FF and write it For j As Integer = 0 To _PAGE_SIZE - 1 page(j) = &HFF Next imageFile.Write(page, 0, _PAGE_SIZE) End If Next imageFile.Close() Catch ex As ConvertException _DisplayUsage(ex.ExitCode, ex.Message) Exit Sub Catch ex As Exception _DisplayUsage(_UNKNOWN_EXCEPTION, ex.Message) Exit Sub End Try End Sub #End Region #Region " Local Methods " Private Shared Sub _DisplayUsage(ByVal exitCode As Integer, ByVal msg As String) Console.WriteLine("Convert8XU " & My.Application.Info.Version.ToString(3)) Console.WriteLine("---------------------------------------------------------------------") Console.WriteLine("Usage: Convert8XU [] ") Console.WriteLine("Converts an OS upgrade file and boot code image to a ROM file.") Console.WriteLine("Despite the name, it can take any OS upgrade file (73U, 8XU).") Console.WriteLine() Console.WriteLine("Examples: Convert8XU 83Plus_1.19.8xu image.rom") Console.WriteLine(" Convert8XU 83Plus_1.19.8xu boot.bin image.rom") Console.WriteLine() Console.Write("ERROR: " & msg) Environment.ExitCode = exitCode End Sub Private Shared Function _GetIntelHexLine(ByRef stream As IO.FileStream) As IntelHexLine Dim array(511) As Byte Dim i As Integer Dim ret As New IntelHexLine 'Retrieve the line of data While stream.Length <> stream.Position array(i) = Convert.ToByte(stream.ReadByte()) i += 1 'Have we come to a newline? If array(i - 1) = &HD OrElse array(i - 1) = &HA Then 'Yes, keep going until we're past it Do array(i) = Convert.ToByte(stream.ReadByte()) i += 1 Loop While stream.Length <> stream.Position AndAlso array(i - 1) <> &HD AndAlso array(i - 1) <> &HA Exit While End If End While 'Convert it to a string and store it ret.RawStringData = System.Text.ASCIIEncoding.ASCII.GetString(array, 0, i) Return ret End Function #End Region End Class