<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>development on HHRA</title><link>https://www.hhra.uk/tags/development/</link><description>Recent content in development on HHRA</description><generator>Hugo -- gohugo.io</generator><language>en</language><lastBuildDate>Sat, 14 Dec 2019 00:00:00 +0000</lastBuildDate><atom:link href="https://www.hhra.uk/tags/development/index.xml" rel="self" type="application/rss+xml"/><item><title>Adventures in Low Level Programming - Text play</title><link>https://www.hhra.uk/2019/12/adventures-in-low-level-programming-text-play/</link><pubDate>Sat, 14 Dec 2019 00:00:00 +0000</pubDate><guid>https://www.hhra.uk/2019/12/adventures-in-low-level-programming-text-play/</guid><description>&lt;p>This is part two of my adventures in low level programming series. In &lt;a href="https://www.hhra.uk/2019/10/adventures-in-low-level-programming">part one&lt;/a> we got our computer to boot and then just sit there spinning it&amp;rsquo;s wheels. It doesn&amp;rsquo;t feel like much of an achievement at the moment, but in this installment we&amp;rsquo;ll start to see something a little more exciting.&lt;/p>
&lt;p>Before we get stuck in, we&amp;rsquo;ll need a little more background though (boo! I know). Obviously at this stage we don&amp;rsquo;t want to be writing the text rendering code ourselves, nor do we want to be worrying about placement of the characters. Lukily BIOS has got our back at this stage in the boot process.&lt;/p>
&lt;h1 id="bios-interrupts">BIOS Interrupts&lt;/h1>
&lt;p>So we know that BIOS has got our back on this, but how on earth do we call it to action? That&amp;rsquo;s where our interrupts come in. Interrupts are a way of &amp;lsquo;interrupting&amp;rsquo; whatever the CPU is doing at the time and tell it to temporarily run a different piece of code before coming back and continuing where it left off. Interrupts can be triggered both by code (as we&amp;rsquo;ll see in a second) and by latency-sensitive hardware such as network cards.&lt;/p>
&lt;p>There is a table of interrupts which maps a number to the location of code in memory to deal with that interrupt. For example, &lt;code>0x10&lt;/code> may point to a location 100 bytes into memory which is where the start of the code to deal with whatever interrupt &lt;code>0x10&lt;/code> was starts.&lt;/p>
&lt;p>Rather than having many many different interrupts, instead it&amp;rsquo;s common to combine multiple functions into groups and select between them in a switch like manner. For example, there may be one group related to screen functions (&lt;code>0x10&lt;/code>) within which you may be able to call functions such as &amp;lsquo;print character&amp;rsquo;, &amp;lsquo;set cursor position&amp;rsquo;, or &amp;lsquo;Write graphics pixel&amp;rsquo;.&lt;/p>
&lt;h1 id="registers">Registers&lt;/h1>
&lt;p>In order to understand how we switch between the options in a particular interrupt, we must also understand one of the most fundamental parts of programming at this low level. Registers! You can think of registers as variables in higher level programming.&lt;/p>
&lt;p>Unfortunately, unlike variables in higher level programming, we&amp;rsquo;re usually limited to only four of them. The registers available on all x86 computers are: &lt;code>ax&lt;/code>, &lt;code>bx&lt;/code>, &lt;code>cx&lt;/code>, &lt;code>dx&lt;/code>. Each of these registers holds one word (two bytes) of data.&lt;/p>
&lt;p>We can also choose to split each register into high and low bytes, effectively giving us 8 byte registers or 4 word registers. We reference each byte by swapping &lt;code>x&lt;/code> for &lt;code>l&lt;/code> (low byte) and &lt;code>h&lt;/code> (high byte).&lt;/p>
&lt;p>Let&amp;rsquo;s take a look at a quick example of what working with these registers looks like in practice. At this point we&amp;rsquo;re not yet ready to print out the contents of registers, so instead you&amp;rsquo;ll just need to trust me that they contain what I say they contain (sorry!).&lt;/p>
&lt;pre tabindex="0">&lt;code>MOV ax, 0x4534 # ax now contains 0x4534 or 100010100110100 in binary.
MOV bl, 0x45 # bx now contains 0x4500 or 100010100000000 in binary.
MOV bh, 0x34 # bx now contains 0x4534 or 100010100110100 in binary.
&lt;/code>&lt;/pre>&lt;p>In each of the above examples, we&amp;rsquo;re using the &lt;code>MOV&lt;/code> operation which is what we use to move data into, out of, and between our registers. It&amp;rsquo;s in the format &lt;code>MOV &amp;lt;destination&amp;gt;, &amp;lt;source&amp;gt;&lt;/code> where source can be hard-coded numbers (as in our example), other registers, or even pointers to memory.&lt;/p>
&lt;h1 id="printing-a-character">Printing a character&lt;/h1>
&lt;p>Now that we&amp;rsquo;ve got the necessary background out of the way we can begin on the exciting part - printing a single character to the screen! Bear with me though, I promise by the end of this part we&amp;rsquo;ll be able to print arbitrary strings out.&lt;/p>
&lt;p>Beginning from where we left off in the last part, let&amp;rsquo;s try calling the bios printing routine by using the interrupt &lt;code>0x10&lt;/code> and function code (that thing we discussed earlier about having multiple functions within a single interrupt) &lt;code>0x0e&lt;/code> to indicate teletype mode.&lt;/p>
&lt;p>In this case, we put the function code in &lt;code>ah&lt;/code> (the high byte of register a) and the ascii code for the letter we wish to print in &lt;code>al&lt;/code>. We then call the &lt;code>0x10&lt;/code> interrupt (screen functions) to actually execute the code.&lt;/p>
&lt;pre tabindex="0">&lt;code>mov ah, 0x0e
mov al, &amp;#39;H&amp;#39;
int 0x10
block:
jmp block
times 510-($-$$) db 0
dw 0xaa55
&lt;/code>&lt;/pre>&lt;p>When we compile and run the above using the same command as last time, you should see the same as before but with a &amp;lsquo;H&amp;rsquo; now displayed.&lt;/p>
&lt;p>&lt;img src="https://www.hhra.uk/img/aillp-h-booted.png" alt="Qemu showing only &amp;ldquo;Booting from harddrive&amp;rdquo; with a &amp;ldquo;H&amp;rdquo; below and then waiting forever">&lt;/p>
&lt;p>We can begin printing whole words by repeating the &lt;code>mov al, H&lt;/code> for each letter we wish to print, for example:&lt;/p>
&lt;pre tabindex="0">&lt;code>mov ah, 0x0e
mov al, &amp;#39;H&amp;#39;
int 0x10
mov al, &amp;#39;e&amp;#39;
int 0x10
mov al, &amp;#39;l&amp;#39;
int 0x10
mov al, &amp;#39;l&amp;#39;
int 0x10
mov al, &amp;#39;o&amp;#39;
int 0x10
block:
jmp block
times 510-($-$$) db 0
dw 0xaa55
&lt;/code>&lt;/pre>&lt;p>(Notice that as we don&amp;rsquo;t change the value of &lt;code>ah&lt;/code> we only need to set it once and then just trigger the interrupt each time we change the value of &lt;code>al&lt;/code>)&lt;/p>
&lt;p>This method is very tedius however, so we really need to find a way to simplify this printing so we can place a series of characters somewhere in memory and then loop through and print that string of characters to the screen. We&amp;rsquo;ll need a little more background again to complete this however, so let&amp;rsquo;s dive in.&lt;/p>
&lt;h1 id="comparing-values">Comparing values&lt;/h1>
&lt;p>We&amp;rsquo;ll need some way to identify the end of a string. The usual way to do this in programming is to append a zero byte to the end. If you&amp;rsquo;ve ever programmed in C you&amp;rsquo;ll probably be familiar with the idea of a null terminated string. In fact, a common cause of bugs is forgetting to null terminate strings and have the print functions over-run into memory further down the line.&lt;/p>
&lt;p>In order to detect this zero byte at the end of our string, we&amp;rsquo;ll need a way of comparing one value against another. Luckily x86 assembly has built in instructions for just this. These functions allow you to conditionally jump and come in many forms - the most common being &lt;code>je&lt;/code> (jump if equal), &lt;code>jne&lt;/code> (jump if not equal), &lt;code>jg&lt;/code> (jump if greater), and &lt;code>jl&lt;/code> (jump if less).&lt;/p>
&lt;p>Let&amp;rsquo;s take a quick look at how that looks. In this example, we&amp;rsquo;ll also use the addition instruction to loop 5 times, printing &lt;code>.&lt;/code> each time, and then exiting.&lt;/p>
&lt;pre tabindex="0">&lt;code>mov ah, 0x0e
mov bl, 0
printloop:
; Check if we&amp;#39;ve printed 5 dots yet.
cmp bl, 5
je block
; Print another dot.
mov al, &amp;#39;.&amp;#39;
int 0x10
; Increment our counter
add bl, 1
; Jump to the beginning of our loop
jmp printloop
block:
jmp block
times 510-($-$$) db 0
dw 0xaa55
&lt;/code>&lt;/pre>&lt;p>Hopefully the above makes sense, although it looks complicated it&amp;rsquo;s just a combination of loops and printing which we&amp;rsquo;ve covered previously. You&amp;rsquo;ll notice some comments in there too, which begin with &lt;code>;&lt;/code>. We&amp;rsquo;ll take a quick look at our comparison code to make sure it&amp;rsquo;s clear what&amp;rsquo;s happening there.&lt;/p>
&lt;pre tabindex="0">&lt;code> cmp bl, 5
je block
&lt;/code>&lt;/pre>&lt;p>We do comparisons in two parts, first we tell the CPU what to compare (&lt;code>cmp bl, 5&lt;/code>) which means we are comparing bl to 5. Notice at this point we don&amp;rsquo;t declare what we want to know (e.g. greater than, less than, etc&amp;hellip;), only &lt;em>what&lt;/em> we are comparing.&lt;/p>
&lt;p>The next line is where we actually take action based on the outcome of the comparison. In this case, we jump to the &lt;code>block&lt;/code> tag if &lt;code>bl&lt;/code> equals 5. You can think of this as &amp;ldquo;&lt;code>jmp&lt;/code> if equals&amp;rdquo;.&lt;/p>
&lt;h1 id="reading-from-memory">Reading from memory&lt;/h1>
&lt;p>The other piece of required background is being able to read from memory. For this part we won&amp;rsquo;t concern ourselves with writing to memory programatically, instead telling the assembler to pre-populate part of our code with a particular value. Let&amp;rsquo;s start with this then, and learn how to pre-populate a piece of memory with a particular string.&lt;/p>
&lt;pre tabindex="0">&lt;code>...
my_string:
db &amp;#39;booting...&amp;#39;,0
times 510-($-$$) db 0
dw 0xaa55
&lt;/code>&lt;/pre>&lt;p>For brevity I&amp;rsquo;ve remove the code previously from the top of the file, represented by &lt;code>...&lt;/code>. You&amp;rsquo;ll notice we start by using &lt;code>my_string:&lt;/code> which looks very much like the labels we&amp;rsquo;ve used previously (&lt;code>block:&lt;/code> and &lt;code>printloop:&lt;/code>). In fact, as far as the assembler is concerned, there is no difference. All these labels allow you to do is reference a specific place in memory by name. It doesn&amp;rsquo;t care whether you are using the specific place in memory to jump execution to or move something into a register.&lt;/p>
&lt;p>We then use the &lt;code>db&lt;/code> that we explained in the last post to place the ascnasm bootloader.asmii characters &amp;lsquo;booting&amp;hellip;&amp;rsquo; and then a zero at the current point in the program. If we compile what we&amp;rsquo;ve got so far and take a look at the hexdump output, we can see our characters in the file:&lt;/p>
&lt;pre tabindex="0">&lt;code>$ nasm bootloader.asm
$ hexdump -C bootloader
00000000 b4 0e b3 00 80 fb 05 74 09 b0 2e cd 10 80 c3 01 |.......t...nasm bootloader.asm.....|
00000010 eb f2 eb fe 62 6f 6f 74 69 6e 67 2e 2e 2e 00 00 |....booting.....|
00000020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
000001f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 aa |..............U.|
00000200
&lt;/code>&lt;/pre>&lt;p>Because our whole compiled program gets loaded into memory, this also means that we&amp;rsquo;ve got out target string in the memory of the computer. Now we just need to actually read it! The code to print the first character of our string looks something like this&lt;/p>
&lt;pre tabindex="0">&lt;code>mov ah, 0x0e
mov al, [my_string]
int 0x10
&lt;/code>&lt;/pre>&lt;p>We&amp;rsquo;ll create a new block named &lt;code>printstringdemo&lt;/code> which we will be using to store the code for the printing we&amp;rsquo;re about to do. This can be placed just above the &lt;code>block:&lt;/code>.&lt;/p>
&lt;pre tabindex="0">&lt;code>printstringdemo:
mov ah, 0x0e
mov al, [my_string]
int 0x10
jmp block
block:
jmp block
&lt;/code>&lt;/pre>&lt;p>We can then modify our line above to jump to &lt;code>printstringdemo&lt;/code> when done instead of &lt;code>block&lt;/code>:&lt;/p>
&lt;pre tabindex="0">&lt;code> ; Check if we&amp;#39;ve printed 5 dots yet.
cmp bl, 5
je printstrprintstringdemoing
&lt;/code>&lt;/pre>&lt;p>If we compile and run that we should get five dots and then the letter &amp;lsquo;b&amp;rsquo;. However right now we don&amp;rsquo;t, this is due to a slight disconnect between our assembler and where our code is loaded into memory. When referencing memory using the &lt;code>[]&lt;/code> operator assembly will, by default, reference memory relative to the beginning of our code. This is all fine and dandy assuming our code was loaded at the first byte of memory. As we know, however, the BIOS needs to store other items such as it&amp;rsquo;s interrupt table before our code.&lt;/p>
&lt;p>It turns out, our code is usually loaded at &lt;code>0x7c00&lt;/code> so referencing memory at address zero doesn&amp;rsquo;t in fact reference anything in our program. Instead, we need to reference &lt;code>0x7c00&lt;/code> &lt;strong>plus&lt;/strong> the offset from the start of our program. Luckily, rather than calculating this every time manually, we can put &lt;code>[org 0x7c00]&lt;/code> at the very top of our program which will tell our assembler to calculate all references by adding &lt;code>0x7c00&lt;/code> to the memory address.&lt;/p>
&lt;p>Placing that line at the top of the file and compiling again does get us the expected outcome of five dots followed by a &amp;lsquo;b&amp;rsquo;. Now we&amp;rsquo;ve got to loop through and print the rest of the characters in our string. To do this, we can re-purpose the &lt;code>bx&lt;/code> register and use it to store the current memory address that we need to print out, like so:&lt;/p>
&lt;pre tabindex="0">&lt;code>printstringdemo:
mov bx, my_string
mov al, [bx]
int 0x10
jmp block
&lt;/code>&lt;/pre>&lt;p>So far our changes have made no functional difference, but they have given us a very useful tool. We can now increment the value in bx to get the second, third, fouth, and so on bytes of my_string. Let&amp;rsquo;s get the second byte now and print five dots followed by an &amp;lsquo;o&amp;rsquo;&lt;/p>
&lt;pre tabindex="0">&lt;code>printstringdemo:
mov bx, my_string
call printcharacter
jmp block
printcharacter:
; Move the value of the current value into bl for printing
mov al, [bx]
; If the value is 0 (indicating the end of the string)
cmp al, 0
; Then jump to printdone
je printdone
; Otherwise print the character
int 0x10
; And then add one to the current address
add bx, 0x01
; And loop
jmp printcharacter
printdone:
; Return to where we were in printstring:
ret
&lt;/code>&lt;/pre>&lt;h1 id="putting-it-all-together">Putting it all together&lt;/h1>
&lt;p>Great! We&amp;rsquo;ve got all the background we need now, and are ready to create a nice usable routine for printing a string to the screen. To use this routine we&amp;rsquo;ll set &lt;code>bx&lt;/code> to the memory location of the first byte of our null-terminated string. When we jump to the routine which will loop through the string one character at a time until we meet the zero byte indicating the end. It will then jump back out of the routine back to where it was called from so we can continue.&lt;/p>
&lt;p>Let&amp;rsquo;s start by creating our block that will demonstrate how we want to use this routine (this should entirely replace the existing printstringemo function):&lt;/p>
&lt;pre tabindex="0">&lt;code>printstringdemo:
mov bx, my_string
call printstring
jmp block
&lt;/code>&lt;/pre>&lt;p>We&amp;rsquo;ve got a new instruction here, &lt;code>call&lt;/code>. Call allows us to &lt;code>jmp&lt;/code> to a routine, but then remember where we were and &lt;code>jmp&lt;/code> back to where we were. Very useful here where we may want to print something out, and then continue with other processing. We&amp;rsquo;ll see how we jump back in a second.&lt;/p>
&lt;p>Now that we know exactly how we want to call our routine, let&amp;rsquo;s create the entrypoint.&lt;/p>
&lt;pre tabindex="0">&lt;code>printstring:
; Initialize interrupt to printing character
mov ah, 0x0e
; Jump to the character printing routine
jmp printcharacter
&lt;/code>&lt;/pre>&lt;p>We now need to implement the character printing routine, which will keep looping until it sees a zero, at which point it will jump to a finishing routine.&lt;/p>
&lt;pre tabindex="0">&lt;code>printcharacter:
; Move the current character to print to al
mv al, [bx]
; Check if the current character to print is zero
cmp al, 0
; If it was zero (indicating end of string), jump to the finished routine
je printdone
; Print the character
int 0x10
; Increment the counter and loop
add bx, 0x01
jmp printcharacter
&lt;/code>&lt;/pre>&lt;p>Finally, we need to return to where we were called from, we do this in the &lt;code>printdone&lt;/code> block as such:&lt;/p>
&lt;pre tabindex="0">&lt;code>printdone:
ret
&lt;/code>&lt;/pre>&lt;p>&lt;code>ret&lt;/code> here is the instruction that tells us to jump back to the last &lt;code>call&lt;/code> function.&lt;/p>
&lt;p>Putting it all together now leaves us with a complete assembly file that looks like:&lt;/p>
&lt;pre tabindex="0">&lt;code>[org 0x7c00]
mov ah, 0x0e
mov bl, 0
printloop:
; Check if we&amp;#39;ve printed 5 dots yet.
cmp bl, 5
je printstringdemo
; Print another dot.
mov al, &amp;#39;.&amp;#39;
int 0x10
; Increment our counter
add bl, 1
; Jump to the beginning of our loop
jmp printloop
printstringdemo:
mov bx, my_string
call printstring
jmp block
printstring:
; Initialize interrupt to printing character
mov ah, 0x0e
; Jump to the character printing routine
jmp printcharacter
printcharacter:
; Move the current character to print to al
mov al, [bx]
; Check if the current character to print is zero
cmp al, 0
; If it was zero (indicating end of string), jump to the finished routine
je printdone
; Print the character
int 0x10
; Increment the counter and loop
add bx, 0x01
jmp printcharacter
printdone:
ret
block:
jmp block
my_string:
db &amp;#39;booting...&amp;#39;,0
times 510-($-$$) db 0
dw 0xaa55
&lt;/code>&lt;/pre>&lt;p>Running which gives us five dots and then our string (&amp;ldquo;booting&amp;hellip;&amp;rdquo;).&lt;/p>
&lt;p>&lt;img src="https://www.hhra.uk/img/finished-screen-print-asm.png" alt="Qemu showing only &amp;ldquo;Booting from harddrive&amp;rdquo; with a &amp;ldquo;H&amp;rdquo; below and then waiting forever">&lt;/p>
&lt;p>aaannnddd finally, that&amp;rsquo;s us done! Lots of work just to print a single string to the screen right? There&amp;rsquo;ll be a much shorter post next time (I promise! I need a break as much as you do) about the layout of our project where we begin to set ourselves up for expanding on what we&amp;rsquo;ve done so far.&lt;/p></description></item><item><title>Adventures in Low Level Programming</title><link>https://www.hhra.uk/2019/10/adventures-in-low-level-programming/</link><pubDate>Sat, 12 Oct 2019 00:00:00 +0000</pubDate><guid>https://www.hhra.uk/2019/10/adventures-in-low-level-programming/</guid><description>&lt;p>Ohh, this is going to be a fun one! And one I’ve played with in the past to varying degrees of success. What I’m trying to achieve here is simple, I want to boot a computer from nothing to running some C code which prints “Hello, World!” to the screen. The C code will be sitting on a FAT32 drive attached to the computer.&lt;/p>
&lt;h2 id="getting-an-environment-setup">Getting an environment setup&lt;/h2>
&lt;p>First things first, we’re going to need to get a development environment up and running. We’ll use QEMU as our emulator to allow us to run our code in a nice user-friendly way. Make sure you can get to the point of typing &lt;code>qemu-system-x86_64&lt;/code> into the terminal and having it open up. On ubuntu that’s as simple as installing via apt.&lt;/p>
&lt;pre tabindex="0">&lt;code># sudo apt install qemu
&lt;/code>&lt;/pre>&lt;p>We&amp;rsquo;ll also need a few compilers. First an assembly compiler, you&amp;rsquo;ll need to be able to run nasm and get the version.&lt;/p>
&lt;pre tabindex="0">&lt;code># nasm --version
NASM version 2.14
&lt;/code>&lt;/pre>&lt;p>We’ll need gcc too. This is installed by default on many distributions. Again, you’ll need to be able to run gcc and get the version.&lt;/p>
&lt;pre tabindex="0">&lt;code># gcc --version
gcc (Ubuntu 8.3.0-6ubuntu1) 8.3.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
&lt;/code>&lt;/pre>&lt;p>Both of these can be achieved on ubuntu by installing the packages through the default repos.&lt;/p>
&lt;pre tabindex="0">&lt;code># sudo apt install gcc nasm
&lt;/code>&lt;/pre>&lt;h2 id="a-little-bit-of-theory">A little bit of theory&lt;/h2>
&lt;p>Before we continue, it’s probably prudent to give a little bit of background to what we’re about to try to achieve. The format of a boot sector is actually fairly simple. It’s a set of data at the very start of a physical disk, usually 512 bytes long, that ends in a magic number &lt;code>0xaa55&lt;/code>. That magic number is what tells BIOS that this is indeed a boot sector and not just some random data on a storage disk.&lt;/p>
&lt;p>Clearly, 512 bytes isn’t very much to work with, so we’ve got to work fast and efficiently to read from a filesystem further on in the disk where we can load whole kilobyte files(!).&lt;/p>
&lt;h2 id="creating-some-boilerplate-code">Creating some boilerplate code&lt;/h2>
&lt;p>First things first, we’ve got to sort out that magic number. Let’s write the simplest assembly program we can that will just hang the computer. Importantly though, it’ll place the magic number at the end of the sector and fill in the rest with &lt;code>0x0&lt;/code>. This will continue to work as we add more and more assembly instructions meaning we never need to think about placing it in the right place.&lt;/p>
&lt;pre tabindex="0">&lt;code>block:
jmp block
times 510-($-$$) db 0
dw 0xaa55
&lt;/code>&lt;/pre>&lt;p>Always read assembly top down, so lets take this line by line and analyze what&amp;rsquo;s happening.&lt;/p>
&lt;pre tabindex="0">&lt;code>block:
&lt;/code>&lt;/pre>&lt;p>This is a label which we can use to navigate programmatically through our code. These are the basic building blocks of all control structures that we&amp;rsquo;ll use. Here we&amp;rsquo;re effectively creating the equivelent of an infinite loop in c:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-c" data-lang="c">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">while&lt;/span>(true) {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#75715e">// do nothing
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">&lt;/span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>the next line of code finishes off this infinite loop by jumping immediately back to the label we put in place previously. This line is in two pieces, &lt;code>jmp&lt;/code> which is what we call an &lt;em>unconditional jump&lt;/em>. We&amp;rsquo;ll cover this in more detail later but there are other kinds of jump where we can say, for example, only jump if register &lt;em>x&lt;/em> is larger than 10. This will give us a foundation for building &amp;lsquo;if&amp;rsquo; statements.&lt;/p>
&lt;pre tabindex="0">&lt;code> jmp block
&lt;/code>&lt;/pre>&lt;p>The final two lines are the ones we need for the boilerplate. The first one fills all but the last two bytes in with &lt;code>0x0&lt;/code>&lt;/p>
&lt;pre tabindex="0">&lt;code>times 510-($-$$) db 0
&lt;/code>&lt;/pre>&lt;p>Although it looks intimidating, when broken down this line isn’t quite as complicated as it looks. &lt;code>times&lt;/code> is the ‘command’ that we’re calling. It effectively has two parameters, so you can think of it like: &lt;code>times &amp;lt;number-of-repetitions&amp;gt; &amp;lt;thing-to-repeat&amp;gt;&lt;/code>. In this case we’re repeating &lt;code>510-$&lt;/code> times. That doesn’t mean much, does it? Well… Let’s break this down even more. We know we want to put two bytes at the end of the file (our magic number), which is why we use 510 here and not 512.&lt;/p>
&lt;p>So what we want to know is how many bytes we need to set to &lt;code>0x0&lt;/code> to fill in space between where we are now and 2 bytes before the end of the file. This is where nasm really helps us as it will keep track of how many bytes through the file we are, that’s what the cryptic &lt;code>$&lt;/code> is. All the dollar does is (at compile time) represent the current position in the assembly file. So what we’re actually doing when we evaluate &lt;code>510-$&lt;/code> is &lt;code>510-&amp;lt;current-bytes-into-file&amp;gt;&lt;/code> which will give us back the number of bytes we would need to fill with zeros to fill in all but the last 2 bytes of a 512-byte program.&lt;/p>
&lt;p>Finally we have the thing we actually want to repeat. In this case &lt;code>db 0&lt;/code> which just says, I want to put a zero byte in the file at this position. Because it&amp;rsquo;s repeated multiple times we end up with multiple zeros.&lt;/p>
&lt;p>All that&amp;rsquo;s left at this point is to actually stick the remaining two bytes at the end. We can use the &lt;code>dw&lt;/code> command for this which stands for &lt;code>data word&lt;/code> (previously we used the &lt;code>db&lt;/code> command which stands for &lt;code>data byte&lt;/code>). We then put the magic number in and we&amp;rsquo;re done.&lt;/p>
&lt;pre tabindex="0">&lt;code>dw 0xaa55
&lt;/code>&lt;/pre>&lt;h2 id="compiling-and-inspecting-our-work">Compiling and inspecting our work&lt;/h2>
&lt;p>Let&amp;rsquo;s save the above assembly into a file called &lt;code>bootloader.asm&lt;/code> and then run &lt;code>nasm bootloader.asm&lt;/code>. If we take a look in our folder, we should now see a new file which is exactly 512 bytes long.&lt;/p>
&lt;pre tabindex="0">&lt;code># ls -lahtr
total 24K
-rw-rw-r-- 1 hhra hhra 55 Oct 06 20:29 bootloader.asm
-rw-rw-r-- 1 hhra hhra 512 Oct 06 20:29 bootloader
&lt;/code>&lt;/pre>&lt;p>Looking in the contents of the file with a piece of software called &lt;code>hexdump&lt;/code> which allows us to print out the raw bytes of a file, we can see that the file is indeed 512 bytes long, &lt;strong>and&lt;/strong> that it ends in our magic code!&lt;/p>
&lt;pre tabindex="0">&lt;code># hexdump bootloader
0000000 feeb 0000 0000 0000 0000 0000 0000 0000
0000010 0000 0000 0000 0000 0000 0000 0000 0000
*
00001f0 0000 0000 0000 0000 0000 0000 0000 aa55
0000200
&lt;/code>&lt;/pre>&lt;p>The way hexdump displays information can be a little weird at first. The first column isn&amp;rsquo;t actually the data in the file at all, it&amp;rsquo;s actually the offset from the start of the file. In this case the first line starts at &lt;code>0000000&lt;/code> which is the beginning of the file. The rest of the first line shows us the first 16 bytes of the file in hexadecimal form. Most of it is zeros apart from the first two bytes which are our opcodes (we&amp;rsquo;ll get to this in a second). The second line then starts from &lt;code>0000010&lt;/code> which, surprise surprise, is 16 represented in hex notation.&lt;/p>
&lt;p>You&amp;rsquo;ll notice the file when run through hexdump doesn&amp;rsquo;t actually look like 512 bytes. That&amp;rsquo;s because hexdump is nice enough to remove duplicate blocks for us and replaces them with a &lt;code>*&lt;/code>. The fourth line in the output then is the last set of bytes in the file.&lt;/p>
&lt;pre tabindex="0">&lt;code>00001f0 0000 0000 0000 0000 0000 0000 0000 aa55
&lt;/code>&lt;/pre>&lt;p>The beginning of this line starts at the 496th byte in the file (&lt;code>00001f0&lt;/code> in decimal). There are 14 bytes of zeros displayed, bringing us to the 510th byte, and finally &lt;code>aa55&lt;/code> which completes our file at exactly 512 bytes. Absolutely perfect.&lt;/p>
&lt;p>Let&amp;rsquo;s jump back for just a moment now to the beginning of that file where we have the only other two non-zero bytes in the whole thing.&lt;/p>
&lt;pre tabindex="0">&lt;code>0000000 feeb 0000 0000 0000 0000 0000 0000 0000
&lt;/code>&lt;/pre>&lt;p>Ignoring the zeros on the end, we need to figure out what &lt;code>feeb&lt;/code> stands for. We know they are x86 OP codes. Because of the endianeness of the system, they&amp;rsquo;re actually displayed in a weird order, the argument is coming before the command. The command we need to look up in that case is &lt;code>eb&lt;/code> &lt;a href="https://c9x.me/x86/html/file_module_x86_id_147.html">which corresponds to&lt;/a> &lt;code>jmp&lt;/code> - big freaking surprise. In particular, the command &lt;code>JMP rel8&lt;/code> which means that we want to jump to a position in the file, relative to the current command, specified using an 8bit value. This 8-bit value which is a signed value, in our example is &lt;code>fe&lt;/code> in two&amp;rsquo;s compliment notiation this corresponds to &lt;code>-2&lt;/code>. At this point in the file the current position of the program counter (which is what the jmp is relative to) is &lt;code>fe&lt;/code> which is two bytes into the file, so jumping two bytes back takes us all the way to the start, completing our loop.&lt;/p>
&lt;p>Feel free to experiment with adding further &lt;code>jmp&lt;/code>&amp;rsquo;s and looking at how the soure code changes. In particuar how the number defining how far back we jump changes each time.&lt;/p>
&lt;h2 id="running-our-code">Running our code&lt;/h2>
&lt;p>Now that we fully understand what&amp;rsquo;s going on underneath, all that&amp;rsquo;s required now is to actually run the code and see what happens. I&amp;rsquo;m afraid it won&amp;rsquo;t be very exciting at this point, but just know that nothing happening is a good sign. If something goes wrong the machine will hard-reset. All it takes now is to attach the 512-byte file to our emulator as though it were a disk and run.&lt;/p>
&lt;pre tabindex="0">&lt;code># qemu-system-x86_64 bootloader
WARNING: Image format was not specified for &amp;#39;bootloader&amp;#39; and probing guessed raw.
Automatically detecting the format is dangerous for raw images, write operations on block 0 will be restricted.
Specify the &amp;#39;raw&amp;#39; format explicitly to remove the restrictions.
&lt;/code>&lt;/pre>&lt;p>This should pop up a new window which looks like this and does absolutely nothing:&lt;/p>
&lt;p>&lt;img src="https://www.hhra.uk/img/aillp-qemu-window.png" alt="Qemu showing only &amp;ldquo;Booting from harddrive&amp;rdquo; and waiting forever">&lt;/p>
&lt;p>It doesn&amp;rsquo;t look like much, but this is a big milestone. we&amp;rsquo;re now executing our own code on a computer with no operating system at all. We&amp;rsquo;re still nowhere near our original goal, but we&amp;rsquo;ve now got a platform on which we can build. In the next installment we&amp;rsquo;ll get text rendering to the screen.&lt;/p></description></item><item><title>Environment Specific Settings Files with Drupal BLT</title><link>https://www.hhra.uk/2019/09/environment-specific-settings-files-with-drupal-blt/</link><pubDate>Fri, 13 Sep 2019 00:00:00 +0000</pubDate><guid>https://www.hhra.uk/2019/09/environment-specific-settings-files-with-drupal-blt/</guid><description>&lt;p>Despite having a big warning at the bottom of the &lt;code>docroot/sites/default/settings.php&lt;/code> file telling you not to add additional settings to that file. The BLT documentation does little do say what the recommended way of conditionally including &lt;code>*.settings.php&lt;/code> files depending on environment variables or some other decider.&lt;/p>
&lt;p>Normally, you would do it by adding code to the effect of the below to &lt;code>docroot/sites/default/settings.php&lt;/code> but since there is such a direct warning not to in the BLT version, I needed to find a better way:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-php" data-lang="php">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">if&lt;/span> (&lt;span style="color:#a6e22e">getenv&lt;/span>(&lt;span style="color:#e6db74">&amp;#39;RUNNING_IN_DOCKSAL&amp;#39;&lt;/span>) &lt;span style="color:#f92672">==&lt;/span> &lt;span style="color:#e6db74">&amp;#39;yes&amp;#39;&lt;/span>) {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">include&lt;/span> $app_root &lt;span style="color:#f92672">.&lt;/span> &lt;span style="color:#e6db74">&amp;#39;/&amp;#39;&lt;/span> &lt;span style="color:#f92672">.&lt;/span> $site_path &lt;span style="color:#f92672">.&lt;/span> &lt;span style="color:#e6db74">&amp;#39;/settings.docksal.php&amp;#39;&lt;/span>;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">if&lt;/span> (&lt;span style="color:#a6e22e">getenv&lt;/span>(&lt;span style="color:#e6db74">&amp;#39;RUNNING_IN_QA&amp;#39;&lt;/span>) &lt;span style="color:#f92672">==&lt;/span> &lt;span style="color:#e6db74">&amp;#39;yes&amp;#39;&lt;/span>) {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">include&lt;/span> $app_root &lt;span style="color:#f92672">.&lt;/span> &lt;span style="color:#e6db74">&amp;#39;/&amp;#39;&lt;/span> &lt;span style="color:#f92672">.&lt;/span> $site_path &lt;span style="color:#f92672">.&lt;/span> &lt;span style="color:#e6db74">&amp;#39;/settings.qa.php&amp;#39;&lt;/span>;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>There is a file called &lt;code>docroot/sites/default/settings/includes.settings.php&lt;/code> which looks like it could be hopeful, however it&amp;rsquo;s clearly not initially setup or intended for this purpose. It has a list of settings files to include:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-php" data-lang="php">&lt;span style="display:flex;">&lt;span>$additionalSettingsFiles &lt;span style="color:#f92672">=&lt;/span> [
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#75715e">// e.g,( DRUPAL_ROOT . &amp;#34;/sites/$site_dir/settings/foo.settings.php&amp;#34; )
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">&lt;/span>];
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>and then a loop to include any in the array:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-php" data-lang="php">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">foreach&lt;/span> ($additionalSettingsFiles &lt;span style="color:#66d9ef">as&lt;/span> $settingsFile) {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">if&lt;/span> (&lt;span style="color:#a6e22e">file_exists&lt;/span>($settingsFile)) {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">require&lt;/span> $settingsFile;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>In order to include custom settings files per environment, I ended up adding a few lines between those blocks of code to dynamically add entries to that array depending on the environment.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-php" data-lang="php">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">// Load local docksal settings to override database connection details.
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">&lt;/span>&lt;span style="color:#66d9ef">if&lt;/span> (&lt;span style="color:#a6e22e">getenv&lt;/span>(&lt;span style="color:#e6db74">&amp;#39;RUNNING_IN_DOCKSAL&amp;#39;&lt;/span>) &lt;span style="color:#f92672">==&lt;/span> &lt;span style="color:#e6db74">&amp;#39;yes&amp;#39;&lt;/span>) {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> $additionalSettingsFiles[] &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#a6e22e">DRUPAL_ROOT&lt;/span> &lt;span style="color:#f92672">.&lt;/span> &lt;span style="color:#e6db74">&amp;#34;/sites/&lt;/span>&lt;span style="color:#e6db74">$site_dir&lt;/span>&lt;span style="color:#e6db74">/settings/settings.docksal.php&amp;#34;&lt;/span>;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">// Load QA specific settings.
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">&lt;/span>&lt;span style="color:#66d9ef">if&lt;/span> (&lt;span style="color:#a6e22e">getenv&lt;/span>(&lt;span style="color:#e6db74">&amp;#39;RUNNING_IN_QA&amp;#39;&lt;/span>) &lt;span style="color:#f92672">==&lt;/span> &lt;span style="color:#e6db74">&amp;#39;yes&amp;#39;&lt;/span>) {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> $additionalSettingsFiles[] &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#a6e22e">DRUPAL_ROOT&lt;/span> &lt;span style="color:#f92672">.&lt;/span> &lt;span style="color:#e6db74">&amp;#34;/sites/&lt;/span>&lt;span style="color:#e6db74">$site_dir&lt;/span>&lt;span style="color:#e6db74">/settings/settings.qa.php&amp;#34;&lt;/span>;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>However, I wasn&amp;rsquo;t entirely sure whether this was the best way or not, so I asked someone at Acquia what they thought. He said:&lt;/p>
&lt;blockquote>
&lt;p>You could extend the logic as described to include an environment specific configuration file if you had lots of settings for each environment and wanted to store them in separate files - as long as the files aren&amp;rsquo;t picked up by BLT scan for settings.&lt;/p>
&lt;/blockquote>
&lt;p>Which pretty much confirms this is the most-correct way of including environment-specific settings files with BLT. The comment &amp;ldquo;as long as the files aren&amp;rsquo;t picked up by BLT scan for settings&amp;rdquo; is worth bearing in mind. This is referring to a piece of text in the &lt;a href="https://docs.acquia.com/blt/install/next-steps/#adding-settings-to-settings-php">Acquia Documentation&lt;/a> &amp;ldquo;Acquia BLT globs the docroot/sites/settings directory to find all files matching a &lt;code>*.settings.php&lt;/code> format&amp;rdquo;. This, of course, means that if you&amp;rsquo;re not careful with the naming of the files you may end up with them being included at all times, which isn&amp;rsquo;t what you want.&lt;/p>
&lt;p>For this reason, I have made sure to name my files &lt;code>settings.&amp;lt;environment&amp;gt;.php&lt;/code> which keeps them safe from that blob. You could also place the settings files in &lt;code>docroot/sites/default&lt;/code> instead of &lt;code>docroot/sites/default/settings&lt;/code> which would keep them out of harms way too.&lt;/p></description></item></channel></rss>