Introduction: Orange PI HowTo: Compile Sunxi Tool for Windows Under Windows

Picture of Orange PI HowTo: Compile Sunxi Tool for Windows Under Windows

PREQUISITES:

You will need

  1. A (desktop) computer running Windows.
  2. An Internet connection.
  3. An Orange PI board.

The last is optional, but I am sure, that You already have it. Otherwise You won't read this instructable.

When You buy the Orange PI single board computer it stays just a piece of dead metal until configured properly. And its main configuration file: "script.bin" is the first key to bring it alive. This file is located in the boot partition of Your bootable SD card. And luckily to us, in most of the Linux distributions from the Official site (http://www.orangepi.org/downloadresources/) this partition is FAT32 and can be easily seen by any Windows computer. It does really simplify things, since there is still no reliable way to write into the Linux ext2 partitions from under the Windows.

Unlucky to us the script.bin configuration file has binary format completely unfriendly for human editing. One needs some kind of software tool in order to decrypt it and crypt back after the necessary modifications have been made. And such a toolset does exist. It is infamous SUNXI-TOOLS. The fly in the ointment is that it is intended to run under Linux and we either have to keep a dedicated Linux-machine for only to use the sunxi-tools, or to find a way how to compile them for windows.

I could simply compile it and share the executable, but one never knows whether they would like to make a fresh release and You will need a new compilation ASAP. So I decided to make a guide how to compile the essential tool from the sources. Lets get started.

Step 1: Download Sunxi-tools

Picture of Download Sunxi-tools

Get the latest (or necessary) version of the sunxi-tools sourcecode.
Go to the URL: https://github.com/linux-sunxi/sunxi-tools and choose to download as zip archive.

Step 2: Unzip the Sourcecode

Picture of Unzip the Sourcecode

Once the download has finished, unzip the sourcecode to the folder of
Your choice. (further I will assume that this folder is c:\sunxitools\, so replace this path by the path of your own).

Step 3: Download Code::blocks

Picture of Download Code::blocks

If You have an installed copy of some operational c++ compiler for windows. and if You know how to use it, You may directly proceed to step 3. Others should get a proper c++ compiler and a shell (IDE) to use it comfortably. The choice of mine is code::blocks for Windows along with preinstalled MinGW toolchain. You may get it from here:
https://sourceforge.net/.../codeblocks-16.01mingw-setup.exe/download

Download and install it.

Step 4: Test Your IDE

Picture of Test Your IDE

To test if things go ok, start codeblocks, click "create a new
project", choose "console application", choose either c or c++, type the title of the checkout project, keep the defaults untouched in the next window and click "finish".

Step 5: Complete Test

Picture of Complete Test

Then click a green triangle on the top panel
of the IDE or use Build->Run menu point. If things went right You should see a message from Your autogenerated "Hello world" application in the black "DOS" window.

If not, it means that the IDE and the compiler aren't working properly and You will have to investigate how to set it right. Probably You will have to download another version of the programming tools or to check their permissions in Your firewall/antivirus software.

Step 6: Create New Project

Picture of Create New Project

Now You should have an operational C/C++ programmer's toolkit and the unpacked sunxi-tools sourcecodes in c:\sunxitools\ folder on Your computer. Its time to assemble a project. Create new project in Your IDE. Choose the plain C (not c++) project of the "console application" type.

Make sure that You are creating project in the c:\sunxitools\ folder
and not in some other place. (E.G. codeblocks tend to make a subfolder with the same name as the project has. So if You have named Your project, say "test", and try to place it in c:\sunxitools\, You may end up with the project gone to c:\sunxitools\test\ if You are not attentive enough.) Sunxi-tools contain several utilities, but for our purpose we will need only one: the so called "fexc" utility.

Step 7: Add Files to Project

Picture of Add Files to Project

Exactly "fexc" utility is responsible for conversion of the script.bin into text format and for the back conversion into binary. Its essential that the executable of this utility has the name "fexc.exe", so its good if You've named Your project as "fexc". However You can use any other name of the project, since You can always rename the executable after the compilation, or either You can choose "Project->Properties" from the top pulldown menu and in the appearing window click "Build targets" tab, and edit the "Output filename" field there to override the executable name.

To Your autogenerated project You should add only five source files:

  • fexc.c
  • script.c
  • script_bin.c
  • script_fex.c
  • script_uboot.c

and eight header files:

  • list.h (move it form c:\sunxitools\include\ folder to c:\sunxitools\ folder)
  • fexc.h
  • script.h
  • script_bin.h
  • script_fex.h
  • script_uboot.h
  • version.h

Be sure to exclude the autogenerated main.c from the project, because fexc.c already has the "int main" function in it. (Remember that any program should have only one main function?).

All the necessary source code files are already in the subfolder, where You have unpacked the sourcecodes to. The header files deserve a pair of words, where to get them. "list.h" - is usually in the "include" subfolder of the unpacked sourcecodes set. "version.h" - just create it Yourself. Put there a string like:

#define VERSION "Win32"

Then save and close the file. (You may decorate it with #define's and #ifdef's if You want.)

If You now try to compile the project it will complain about lots of errors and one missing file. The errors are mostly due to a bit of excessive style freedom, the sunxi-tools programmers used to apply, and the missing file is the dependency not included into the pack of the source code. Lets deal with this step by step.

Step 8: Have Gcc Follow the 1999 ISO C Language Standard

Picture of Have Gcc Follow the 1999 ISO C Language Standard

In order for the compiler not complain the too free programming style set the "с99" standart of the compilation. In codeblocks go to the "Project -> Build Options" menu and in the "Compiler Settings -> Compiler Flags" check the "Have gcc follow the 1999 ISO C language standard" checkbox. Or You can just add "-std=c99" into Your compiler options string.
Now if You try to compile the project those tons of errors should begone and You are one to one with the missing dependency.

Step 9: Find the Missing Dependency

Picture of Find the Missing Dependency

The missing dependency is "mman.h" file - the header of some kind of linux memory manager. Windows C natively has no such file, but fortunately there is a windows port of it. Go to https://github.com/witwall/mman-win32 for windows. Download the snapshot of git repository.

Step 10: Unpack the Mman

Picture of Unpack the Mman

Unpack the mman.c and mman.h files, place them into the c:\sunxitools\ folder.

Step 11: And Add Them to the Project

Picture of And Add Them to the Project

Step 12: Correct Path

Picture of Correct Path

And in file "fex.c" raplece line:

#include <sys/mman.h>

to the

#include "mman.h"

At this step Your compiler should not complain anything and You will get the long wait fexc.exe as the
output. Dont be happy too early. The utility is still not fully functional. You may ensure this by decrypting some valid script.bin file into the text form - script.fex file with consequent encrypting the script.fex file back into the script.bin. You may note that the size of the resulting script.bin differs slightly from the size of the original script.bin. And if You try to decrypt the resulting once again it will fail. Neither the Orange PI will work with this script.bin. To get the functional utility we have to discharge a code bomb, that someone has put into the sunxi-tools sourcecode. It will be our next step.

Step 13: Exorcism

Picture of Exorcism

In order to discharge the code bomb open the fexc.c code file and find there a text string of the next content:

else if ((out = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0) {

Just replace it with the next string:

else if ((out = open(filename, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 512))<0){

If not the evil digits "666" in the first string I'd think that the coder has just forgot to use the O_BINARY flag. But the Number of The Beast does clarify his intentions transparently. Go figure, how ingenious it is: due to the subtle difference in how the files are processed in Windows and Linux the bomb has no effect when the utility is compiled and used under the Linux. But it ruins everything when the utility is used under Windows.

After the bomb has been disarmed, You can finally compile and safely use the fexc utility on Your Windows desktop computer.

Step 14: NOTES

Picture of NOTES

1) To use the fexc utility comfortably, You should get two batch files:

bin2fex.bat - and - fex2bin.bat.

You can get them from some faily fexc.exe build for Windows out there, or either You can type them Yourselves:

  • bin2fex.bat should contain "fexc -I bin -O fex script.bin script.fex"
  • fex2bin.bat should contain "fexc -O bin -I fex script.fex script.bin"

2) If it is difficult to find the mman manager for Windows one can avoid its usage at all. However it takes much more editing of the fexc.c file and requres at least some knowledge of c. For Your convinience I share the edited sourcecode of the fexc from the sunxi-tools v1.4 free from the dependency to mman.h along with codeblocks project file and with sample script.bin from some orange pi. You can download fexc_nomman.zip

3) It is possible that in consequent versions of sunxi-tools they will add some more dependencies. Feel free to find them over the internet and add them to Your compilation project.

5) Finally here is the precompiled version of fexc.exe for Win32:

fexc_nomman.zip

If You are lazy enough feel free to use ver. However beware that it won't be updatet if/when the newer versions of SunxiTools/Windows will be available. So its better to learn how to compile them than to depend on some fixed binary build, I presume.

4) The "Orange PI", "Code::Blocks", "Windows", "Linux", "Sunxi-Tools", "Allwinner", etc... are the correspondent trademarks of their respective owners.

Comments

Swansong (author)2017-11-07

Great first instructable, thanks for sharing!

Laphicet (author)Swansong2017-11-11

Thanks, Glad You like it. I gonna make a series of them from the very beginning (this one) to 3D programming for OPI (will be the last one).

About This Instructable

690views

14favorites

Bio: Unknown
More by Laphicet:Orange PI HowTo: Set It Up to Use With a 5" HDMI TFT LCD DisplayOrange PI HowTo: Set It Up to Use With Car Rearview Display and HDMI to RCA AdapterOrange PI HowTo: Compile Sunxi Tool for Windows Under Windows
Add instructable to: