Orange PI HowTo: Compile Sunxi Tool for Windows Under Windows

by Laphicet in Circuits > Computers

5321 Views, 19 Favorites, 0 Comments

Orange PI HowTo: Compile Sunxi Tool for Windows Under Windows

ridble-orange-pi-logo_.png

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.

Download Sunxi-tools

S1_01.png
S1_02.png

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

Unzip the Sourcecode

S11.png

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).

Download Code::blocks

S4.png

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.

Test Your IDE

S3.png

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".

Complete Test

S5.png

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.

Create New Project

S6_01_console.png
S6_02_C.png
S6_03_fexc.png
S6_04_GCC.png

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.

Add Files to Project

S7.png

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 seven 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.

Have Gcc Follow the 1999 ISO C Language Standard

S8.png

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.

Find the Missing Dependency

S9.png

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.

Unpack the Mman

S10_unpack_mman.png

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

And Add Them to the Project

S11.png

Correct Path

S13.png

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.

Exorcism

S12.png

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.

NOTES

S14.png

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.

5) If You compiler complains about not finding mman functions, like:

undefined reference to '_imp__mmap'

be aware that define lovers of the mman development community have forgotten that the code can be compiled not only as dll library. It can also be a static library or a standalone code like we have here. To fix the problem edit "mman.h" file as follows:

a) find the strings:

#if defined(MMAN_LIBRARY)
#define MMANSHARED_EXPORT __declspec(dllexport)
#else
#define MMANSHARED_EXPORT __declspec(dllimport)
#endif

b) add the string

#define MMANSHARED_EXPORT

just below the strings found at the previous step