Skip to content

Commit ca18738

Browse files
committed
Initial import
0 parents  commit ca18738

18 files changed

+2548
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.vs
2+
bin
3+
obj

Example/ExampleProgram.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using FirmwareFile;
2+
using System;
3+
4+
namespace FirmwareFiles.Example
5+
{
6+
class ExampleProgram
7+
{
8+
public static void Main( string[] args )
9+
{
10+
if( args.Length > 0 )
11+
{
12+
PrintFirmwareFileInfo( args[0] );
13+
}
14+
else
15+
{
16+
var exeName = System.IO.Path.GetFileName( System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName );
17+
Console.WriteLine( $"USAGE: {exeName} <firmware_file_path>" );
18+
}
19+
}
20+
21+
private static void PrintFirmwareFileInfo( string filepath )
22+
{
23+
try
24+
{
25+
var firmware = IntelFileLoader.Load( filepath );
26+
27+
foreach( var fwBlock in firmware.Blocks )
28+
{
29+
Console.WriteLine( $"Memory block: StartAddress=0x{fwBlock.StartAddress:X8} EndAddress=0x{fwBlock.StartAddress + fwBlock.Size:X8} Size=0x{fwBlock.Size:X} ({fwBlock.Size})" );
30+
}
31+
}
32+
catch( Exception e )
33+
{
34+
Console.WriteLine( $"ERROR: {e.Message}" );
35+
}
36+
}
37+
}
38+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net5.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<ProjectReference Include="..\Lib\FirmwareFile.csproj" />
10+
</ItemGroup>
11+
12+
</Project>

FirmwareFile.sln

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.31613.86
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FirmwareFile", "Lib\FirmwareFile.csproj", "{131B9395-C187-44E8-BD97-4A5BE418D9EB}"
7+
EndProject
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FirmwareFile.Tests", "Tests\FirmwareFile.Tests.csproj", "{E65174FA-36C3-4F3A-B8BE-952871743ABF}"
9+
EndProject
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FirmwareFiles.Example", "Example\FirmwareFiles.Example.csproj", "{03438EC9-5BDF-41EA-9B66-A656E6FD2154}"
11+
EndProject
12+
Global
13+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
14+
Debug|Any CPU = Debug|Any CPU
15+
Release|Any CPU = Release|Any CPU
16+
EndGlobalSection
17+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
18+
{131B9395-C187-44E8-BD97-4A5BE418D9EB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
19+
{131B9395-C187-44E8-BD97-4A5BE418D9EB}.Debug|Any CPU.Build.0 = Debug|Any CPU
20+
{131B9395-C187-44E8-BD97-4A5BE418D9EB}.Release|Any CPU.ActiveCfg = Release|Any CPU
21+
{131B9395-C187-44E8-BD97-4A5BE418D9EB}.Release|Any CPU.Build.0 = Release|Any CPU
22+
{E65174FA-36C3-4F3A-B8BE-952871743ABF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23+
{E65174FA-36C3-4F3A-B8BE-952871743ABF}.Debug|Any CPU.Build.0 = Debug|Any CPU
24+
{E65174FA-36C3-4F3A-B8BE-952871743ABF}.Release|Any CPU.ActiveCfg = Release|Any CPU
25+
{E65174FA-36C3-4F3A-B8BE-952871743ABF}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{03438EC9-5BDF-41EA-9B66-A656E6FD2154}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{03438EC9-5BDF-41EA-9B66-A656E6FD2154}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{03438EC9-5BDF-41EA-9B66-A656E6FD2154}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{03438EC9-5BDF-41EA-9B66-A656E6FD2154}.Release|Any CPU.Build.0 = Release|Any CPU
30+
EndGlobalSection
31+
GlobalSection(SolutionProperties) = preSolution
32+
HideSolutionNode = FALSE
33+
EndGlobalSection
34+
GlobalSection(ExtensibilityGlobals) = postSolution
35+
SolutionGuid = {30E919E4-DB19-4809-B5C0-D838207D5E16}
36+
EndGlobalSection
37+
EndGlobal

LICENSE.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Copyright (c) 2020, Jesus Gonzalez <[email protected]>
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are met:
6+
7+
1. Redistributions of source code must retain the above copyright notice, this
8+
list of conditions and the following disclaimer.
9+
10+
2. Redistributions in binary form must reproduce the above copyright notice,
11+
this list of conditions and the following disclaimer in the documentation
12+
and/or other materials provided with the distribution.
13+
14+
3. Neither the name of the copyright holder nor the names of its
15+
contributors may be used to endorse or promote products derived from
16+
this software without specific prior written permission.
17+
18+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Lib/FirmwareFile.csproj

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
<RootNamespace>FirmwareFile</RootNamespace>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
11+
<_Parameter1>FirmwareFile.Tests</_Parameter1>
12+
</AssemblyAttribute>
13+
</ItemGroup>
14+
15+
</Project>

Lib/Sources/BinaryFileLoader.cs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* @file
3+
* @copyright Copyright (c) 2020 Jesús González del Río
4+
* @license See LICENSE.txt
5+
*/
6+
7+
using System;
8+
using System.IO;
9+
using System.Threading.Tasks;
10+
11+
namespace FirmwareFile
12+
{
13+
/**
14+
* Loader for binary firmware files (i.e., a block of contiguous memory without
15+
* an explicit address).
16+
*/
17+
public class BinaryFileLoader
18+
{
19+
/*===========================================================================
20+
* PUBLIC METHODS
21+
*===========================================================================*/
22+
23+
/**
24+
* Loads a firmware from the file at the given path.
25+
*
26+
* @param [in] filePath Path to the file containing the firmware
27+
*/
28+
public static Firmware Load( string filePath )
29+
{
30+
return LoadAsync( filePath ).GetAwaiter().GetResult();
31+
}
32+
33+
/**
34+
* Loads a firmware from the file at the given stream.
35+
*
36+
* @param [in] stream Stream to provide the firmware file contents
37+
*/
38+
public static Firmware Load( Stream stream )
39+
{
40+
return LoadAsync( stream ).GetAwaiter().GetResult();
41+
}
42+
43+
/**
44+
* Loads asynchronously a firmware from the file at the given path.
45+
*
46+
* @param [in] filePath Path to the file containing the firmware
47+
*/
48+
public static Task<Firmware> LoadAsync( string filePath )
49+
{
50+
var fileStream = new FileStream( filePath, FileMode.Open, FileAccess.Read );
51+
52+
return LoadAsync( fileStream );
53+
}
54+
55+
/**
56+
* Loads asynchronously a firmware from the file at the given stream.
57+
*
58+
* @param [in] stream Stream to provide the firmware file contents
59+
*/
60+
public static async Task<Firmware> LoadAsync( Stream stream )
61+
{
62+
var fwFile = new Firmware( false );
63+
64+
int dataSize = (int) stream.Length;
65+
66+
byte[] data = new byte[dataSize];
67+
68+
if( await stream.ReadAsync( data, 0, dataSize ) != dataSize )
69+
{
70+
throw new Exception( "Couldn't read binary file contents" );
71+
}
72+
73+
fwFile.SetData( 0, data );
74+
75+
return fwFile;
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)