Coding in HEX


39 posts   Page 1 of 2   1, 2
by HEX_addict » Sat Jun 21, 2014 12:35 am
Hy! Can somebody give me the most simple code in HEX for RPi, model B, ver.2?
I want to write it in sector 0 of an empty SD Card, and trying to see if something hapen.
The code can be everything: "Switch ON the OK Led", "Print a character on screen" etc.
If is posible to look like this (as little endian 32 bit words):
00000000: 00010203 04050607 08090a0b 0c0d0e0f ...
Posts: 14
Joined: Wed Jun 11, 2014 12:40 pm
Location: Romania
by mahjongg » Sat Jun 21, 2014 12:58 am
Note that the boot code (i'm assuming that this is what you are trying to test) ISN'T written in ARM code, but instead is code written for the Graphics processing core, and almost nobody knows how that code works!
So writing data to sector 0 of an empty SD Card, and trying to see if something happen is pointless, as ARM hex code there won't work! Also the PI has executed at boot time not a byte of code to initialize stuff like the video system, because all this must also be done using the gpu, not the ARM cpu.
User avatar
Forum Moderator
Forum Moderator
Posts: 4889
Joined: Sun Mar 11, 2012 12:19 am
by rpdom » Sat Jun 21, 2014 6:00 am
Actually, sector 0 contains an MS-DOS style MBR and partition table. It can be used to boot quite a few OSen on a PC, but the boot block isn't used at all on the Pi, apart from possible to identify the fact that it is an MS-DOS type MBR and has a valid partition table.

The only bit that the Pi needs in it is that partition table to show that the first partition is FAT and where it is and how big.

No executable code is used from sector 0.

For creating "simple" code to run on a Pi, look at the Assembler tuts at http://www.cl.cam.ac.uk/freshers/raspbe ... orials/os/ Programming in hex is possible, but would only be useful as an exercise as you would essentially be doing by hand what the assembler does for you.

I used to write Z80 code in hex on ZX80/ZX81/ZX Spectrum and Sharp MZ-80K. Ouch, it was tedious and prone to error. Having to recalculate the all relative jump instructions every time I added or removed a byte here or there...
Posts: 3069
Joined: Sun May 06, 2012 5:17 am
Location: Essex, UK
by HEX_addict » Sat Jun 21, 2014 9:08 am
Actually, I read already the tutorial for Baking Pi – Operating Systems Development, but I didn't understand nothing. I instal that Toolchain, but I never understand how to use it. So I decide to program my Raspberry Pi only in HEX. I want to write an OS impossible to brake, so only in HEX I can see exactly what I write.
Before I write small OS in Boot sector, on HDD, on x86 using only 8086 instruction set, using Debug program from Windows, and it was very easy for me, but I see on ARM is too hard too understand. :cry:

The only thing I find for boot is this: "After the Raspberry Pi boot sequence loads the kernel.img
and jumps to the address 0x8000, the Raspberry Pi bootloader has served its purpose and the control is given to the
Linux kernel."
Posts: 14
Joined: Wed Jun 11, 2014 12:40 pm
Location: Romania
by rpdom » Sat Jun 21, 2014 9:29 am
HEX_addict wrote:The only thing I find for boot is this: "After the Raspberry Pi boot sequence loads the kernel.img
and jumps to the address 0x8000, the Raspberry Pi bootloader has served its purpose and the control is given to the
Linux kernel."


This is exactly what you need to know.

Encode your "HEX" stuff to run at 0x8000, save it as kernel.img (preferable in ELF format - look it up). Boot.

Seriously, you should look at the Baking Pi tutes, they aren't that difficult and you will pick up ARM code quite quickly if you forget x86 and the way that works.

ARM code is one of the leanest and neatest instruction sets I've used (and that is quite a lot). Learn the instruction set and you will get used to it. I've been using it on and off since the very first ARM chip was released :)
Posts: 3069
Joined: Sun May 06, 2012 5:17 am
Location: Essex, UK
by HEX_addict » Sat Jun 21, 2014 10:39 am
rpdom wrote:Encode your "HEX" stuff to run at 0x8000, save it as kernel.img (preferable in ELF format - look it up). Boot.

Seriously, you should look at the Baking Pi tutes, they aren't that difficult and you will pick up ARM code quite quickly if you forget x86 and the way that works.

ARM code is one of the leanest and neatest instruction sets I've used (and that is quite a lot). Learn the instruction set and you will get used to it. I've been using it on and off since the very first ARM chip was released :)


ELF format = Litle Endian Format ??

Can u write for me that "HEX stuff"? Plsss. Make a small HEX program to show on screen the text "Hy!", or evrything small and easy to understand. I will write your code with Hxd Hexeditor in Sector 0 to see if something happen.

Beleave me I read the first 2 Lessons from Baking Pi, I watch also https://www.youtube.com/watch?v=U9H7TmRt64A, but I can start. I will atached my code i write in Boot Sector, but doesn't work:
00000000: 02 02 A0 E3 02 16 A0 E3 01 00 80 E1 01 17 A0 E3 04 10 80 E5 01 18 A0 E3 28 10 80 E5 FE FF FF EA
00000020: 00 00 00 00 00 00 00 00 ....
..........................................................
000001E0: 00 00 00 .... 00 55 AA
00000200: 00 00 00 00 .......................
Posts: 14
Joined: Wed Jun 11, 2014 12:40 pm
Location: Romania
by ghans » Sat Jun 21, 2014 10:56 am
As you were told , the "boot sector" of the SD card is
completely ignored. Only the contents of the file kernel.img
count.
I think you just need to type the code in the video in
your HEX editor and save it as kernel.img.

Most people here use ARM Assembly , and there is virtually zero
documentation on "coding in HEX". So at least a basic
understanding of ARM assembly is important to translate any
advanced examples in your head to binary/"HEX code".

ELF stands for http://en.m.wikipedia.org/wiki/Executab ... ble_Format ,
btw.

ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org
Posts: 4375
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany
by HEX_addict » Sat Jun 21, 2014 12:47 pm
I understand. So, I must to quit programming. I will sell my Raspberry Pi and I wil continue programming with Debug from Windows on x86 processors. :cry:
In case u find some good information about WRITING IN HEX ON RASPBERRY PI, please post it here.
Posts: 14
Joined: Wed Jun 11, 2014 12:40 pm
Location: Romania
by ghans » Sat Jun 21, 2014 2:33 pm
Thats NOT what i said. I just meant learning ARM assembly will make your life easier. The video you linked to shows how "code up" things
without it , but it is the first tutorial of its kind that i have seen. Most seem to deal with writing assembly instead.

People wrote computer programs before assemblers were invented and the video shows that you still can do this on a Pi.
I wish you good luck , but be aware that this is mostly "uncharted territory" - you being one of the first cartographs.

ghans
Last edited by ghans on Sat Jun 21, 2014 2:41 pm, edited 1 time in total.
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org
Posts: 4375
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany
by mahjongg » Sat Jun 21, 2014 2:34 pm
rpdom wrote:Actually, sector 0 contains an MS-DOS style MBR and partition table. It can be used to boot quite a few OSen on a PC, but the boot block isn't used at all on the Pi, apart from possible to identify the fact that it is an MS-DOS type MBR and has a valid partition table.

The only bit that the Pi needs in it is that partition table to show that the first partition is FAT and where it is and how big.

No executable code is used from sector 0.
you are quite right rpdom, I did understood what HEX_adddict was trying to do, but obviously his thinking wasn't actually based on factual information. I stand corrected.

I remember a colleague that could read a hex-dump of ZX80 code, as if it was assembler. Me, I never remembered more than that "C9" was the return instruction and "CD xx yy" was the call instruction (if memory serves me well). Also I used lined notebook paper to hand assemble 6502 code for my KIM-1, which I then punched into its hexadecimal keypad. that was fun! but very laborious.
User avatar
Forum Moderator
Forum Moderator
Posts: 4889
Joined: Sun Mar 11, 2012 12:19 am
by HEX_addict » Sat Jun 21, 2014 3:36 pm
So, I'm alone. :(
Do you know maybe, after GPU load the boot sector into adress 0x8000, who will execute the code from memory adress 0x8000, the GPU or CPU? Because if is execute from the GPU, I can not write code in ARM instruction set, right?
Posts: 14
Joined: Wed Jun 11, 2014 12:40 pm
Location: Romania
by ghans » Sat Jun 21, 2014 3:40 pm
After the GPU is up , the ARM takes over. You can write ARM code into kernel.img and it will be excuted from 0x8000.
Note that kernel.img is just a file on a FAT filesystem. No need to use low-level disk writing.

ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org
Posts: 4375
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany
by mahjongg » Sat Jun 21, 2014 3:43 pm
So, I'm alone. :(

Hardly!

After the GPU has booted and initialized the video system it transfers control to the ARM, which starts executing code from the 0x8000 address, where the GPU has placed the code read from the SD-card from the file "kernel.img". Normally this code contains the linux kernel (that's why it is called "kernel.img"), but it can contain any kind of ARM code!

This is specifically "bare metal" terrain, that is the reason why we have a bare metal forum, where this thread IMHO belongs!

also note that all the "BIOS interrupts" you might be familiar with to do things like basic terminal I/O are not present in a PI, which has no BIOS! Normally this would be catered for by the Linux kernel, but since that isn't loaded, yes you are "alone" in that sense! That's why you need bare metal knowledge for even the simplest tasks, perhaps excluding blinking the ACT LED, which is connected to a GPIO-16 (active low).
User avatar
Forum Moderator
Forum Moderator
Posts: 4889
Joined: Sun Mar 11, 2012 12:19 am
by rpdom » Sat Jun 21, 2014 5:17 pm
mahjongg wrote:I remember a colleague that could read a hex-dump of ZX80 code, as if it was assembler. Me, I never remembered more than that "C9" was the return instruction and "CD xx yy" was the call instruction (if memory serves me well).

Ah, those were the days. Yes, CD was the call instruction and C3 was the jump instruction. 00 was noop. I can't remember any of the others these days, but it has been a long time (goes all misty eyed now).
Posts: 3069
Joined: Sun May 06, 2012 5:17 am
Location: Essex, UK
by HEX_addict » Sat Jun 21, 2014 7:17 pm
Yap! Maybe i will try again when I will find somebody who made already a small OS in ARM. For the moment, I quit. But I will stay in stand'by. Never say never! 10x for help anyway. :(
Posts: 14
Joined: Wed Jun 11, 2014 12:40 pm
Location: Romania
by mahjongg » Sat Jun 21, 2014 7:43 pm
moved topic to bare metal forum, where its the most appropriate, kick me when I've done wrong. :P
User avatar
Forum Moderator
Forum Moderator
Posts: 4889
Joined: Sun Mar 11, 2012 12:19 am
by ghans » Sat Jun 21, 2014 9:23 pm
@HEX_addict

Did the video tutorial not work ?

ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org
Posts: 4375
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany
by HEX_addict » Sat Jun 21, 2014 10:12 pm
No, didn't work. :(
He have 2 mistake in his tutorial, but I correct them. He said something, but write diferent. Also, he use another functions , not the original asm code from oficial Baking Pi - Lesson 1. The code from his youtube tutorial I write it above.
By the way, I format the SD card, I put (with copy-paste) only the file ".bin" from RISC OS card, witch I use (because have BBC BASIC with a easy ARM Assembler), and... surprise: when i boot from the ".bin SD card", nothing show me on screen, but Green LED OK/ACK is blinking. :))))))
I saw on the internet that with Bare Metal u can make your own Operating System, but when I saw that tutorials are in C++, I quit. I know a litle VB 6.0 and I work before in x86 ASM, with Debug.
Posts: 14
Joined: Wed Jun 11, 2014 12:40 pm
Location: Romania
by rpdom » Sun Jun 22, 2014 7:30 am
Glad you got it working :)

The BBC BASIC built-in ARM assembler was how I started in ARM code back in the 1980s, but as I mostly use Linux now I use (g)as which I'm finding usable.

If you want things to appear on the screen you will have to write your own screen driving routines. As someone mentioned earlier there is no BIOS to handle the basic hardware functions like you do on old x86 systems, and yes, I've used both debug and BBC BASIC (x86) assembler to write various x86 code and small "embedded" stuff.

To print a character on the screen you'll need to use some of the "mailbox" routines (they are one way the ARM CPU can exchange information with the GPU) to get/set a framebuffer size/depth/start-address. Then you store bytes in memory at offsets from that start-address to plot pixels on the screen.

I would paste some example code, but I'm away from home and haven't got any with me.
[edit]
I've never been able to fully understand C code, but I'm absolutely fine with assembler - I can see what it's doing rather than using arcane symbols and complex terminology ;)
Posts: 3069
Joined: Sun May 06, 2012 5:17 am
Location: Essex, UK
by HEX_addict » Sun Jun 22, 2014 11:14 pm
That's why I hate also C, and because of C exist hackers. If I have somebody to help me I can build a server imposible to brake.
I will do it anyway starting from next year when I will have enough money to eat, and stay relax.
I need only one month to read books of Assembler for x86, one month to makesmall exercises in ASM, and one month to programming the server.
Sometimes I'm asking...how much cost me a machine who can build me my own processor/SOC and my own Raspberry Pi? I can make it 10-20 times faster only by chanching the logic from inside him. I lose time from my brain trying to find how the processor working, instead make programs on Raspberry Pi. If a company will sell a kit to make your own Raspberyy Pi, i will change the world. Windows and Linux will be just history. I cane make every kind of program, but only in HEX. :(
If u find time, put here the code in HEX and ASM, to print a character on screen.
Posts: 14
Joined: Wed Jun 11, 2014 12:40 pm
Location: Romania
by FLYFISH TECHNOLOGIES » Sun Jun 22, 2014 11:42 pm
Hi,
HEX_addict wrote:That's why I hate also C, and because of C exist hackers.

Hacker != Cracker.

HEX_addict wrote:If I have somebody to help me I can build a server imposible to brake.

As they say... the only server impossible to break is the one disconnected/powered off and locked in a dark room.

HEX_addict wrote:I need only one month to read books of Assembler for x86, one month to makesmall exercises in ASM, and one month to programming the server.

Ok, great. Good luck.

HEX_addict wrote:how much cost me a machine who can build me my own processor/SOC and my own Raspberry Pi?

Enough that you don't want to own that "machine", but you will (probably next year after you'll finish reading ASM book) outsource the production to one of the existing factories in this business.

HEX_addict wrote:I can make it 10-20 times faster only by chanching the logic from inside him.

Do you have any web link where we can see your past achievements ?

HEX_addict wrote:If a company will sell a kit to make your own Raspberyy Pi, i will change the world.

Interesting idea... To what blocks would you break down the RasPi to get this kit ?

HEX_addict wrote:Windows and Linux will be just history.

After each sentence I like you even more than before...

HEX_addict wrote:I cane make every kind of program, but only in HEX. :(

Perfect choice ! HEX is actually the best selection for coding and maintaining.

HEX_addict wrote:If u find time, put here the code in HEX and ASM, to print a character on screen.

C'mon... don't waste your potential with printing a character on the screen.


Best wishes, Ivan Zilic.
Running out of GPIO pins and/or need to read analog values?
Solution: http://www.flyfish-tech.com/FF32
User avatar
Posts: 1541
Joined: Thu Oct 03, 2013 7:48 am
Location: Ljubljana, Slovenia
by DougieLawson » Mon Jun 23, 2014 9:17 am
HEX_addict wrote: If a company will sell a kit to make your own Raspberyy Pi, i will change the world. Windows and Linux will be just history.


That sounds like a plot for the next James Bond movie.

Best of luck, it's taken Linus (and an army of acolytes) since 1991 to get their OS to the current status. So I'll look forward to your new super secure OS appearing in 2037.
Hacker on ZX80 and Microtan65
Unemployed mainframe database specialist.
Linux hacker since 1995.
RPi owner since 2012.
Twitter: @DougieLawson

Gaffer tape is "The Force", it has a dark side and a light side and it holds the Universe together.
User avatar
Posts: 5351
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
by ame » Mon Jun 23, 2014 10:04 am
DougieLawson wrote:
HEX_addict wrote: If a company will sell a kit to make your own Raspberyy Pi, i will change the world. Windows and Linux will be just history.


That sounds like a plot for the next James Bond movie.

Best of luck, it's taken Linus (and an army of acolytes) since 1991 to get their OS to the current status. So I'll look forward to your new super secure OS appearing in 2037.


He might get almost a year's worth of use from it then...
Posts: 854
Joined: Sat Aug 18, 2012 1:21 am
Location: Japan
by dwelch67 » Mon Jun 23, 2014 1:37 pm
I assume when you say hex you mean machine code?

Actually a number of us have been able to program the GPU using its instruction set thanks to HH and his work and the folks helping out there, now I believe Broadcom has published that info.

Writing in "HEX" wont in any way shape or form make your operating system safe or protected or anything like that. That isnt possible on this or most hardware on the planet, and if it were then it is only possible for a short period of time, these days that is anywhere from hours to weeks (once there is interest). The time to defeat protection is generally a fraction of the time to create it. This form of hacking happens at the machine code level (generally disassembled).

The kernel.img file is not an .elf file it is just a raw binary so if for example you take the instruction for branch to self 0xEAFFFFFE and you place it in a file with the proper endianness it will actually run, now it wont do anything very exciting but it wont crash either.

it is harder, esp if you are learning an instruction set to learn machine code first, then assembly later than to learn assembly first then machine code later. Knowing C makes the transition to a new instruction set even easier because you can compile and disassemble and learn the assembly language from that as well...

It is quite trivial to generate machine code if that is what you mean by hex for the raspberry pi. The documentation is all out there for the arm instruction set the kernel.img file is just the raw memory image that is loaded into the ram. Just put the bytes in the file and power it on.

The arm instruction set is significantly easier than the x86 instruction set, much easier to program, if you can program x86 machine code you can do pretty much anything else.

If you cant operate an assembler or compiler you are not going to get very far anyway, spend a few more seconds and get those working. For example, simply load an sd card with the off the shelf raspbian or whatever os, which has the tools already built and ready to use. Or just get the pre-built ones from codesourcery (now mentor graphics) or https://launchpad.net/gcc-arm-embedded. Much easier than trying to encode branches and other instructions if you dont have a working assembler to show you how to do it. You only need binutils if you dont want to use C.
Posts: 401
Joined: Sat May 26, 2012 5:32 pm
by HEX_addict » Mon Jun 23, 2014 7:35 pm
FLYFISH TECHNOLOGIES wrote:Best wishes, Ivan Zilic.

I was funy right? :| Butttt.... If I was speak right? :|
You know, you have right. I will make a site where i will put my exercises in x86 HEX & ASM. I will sell All my books and things, to have money to eat, and I will keep only my bed, my TV, my cheap China chear , my out of order table, my pet fishes and my computers. From tomorrow I will wake up at 8:00 and I will sleep at 22:00-23:00. Three months I said?...3 months will be.
God please, be with me!
Please, don't close here till I will put the link from my site.

Best wishes, Omu.
Posts: 14
Joined: Wed Jun 11, 2014 12:40 pm
Location: Romania