<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Websofia</title>
	<atom:link href="http://www.websofia.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.websofia.com</link>
	<description>Notes on programming</description>
	<lastBuildDate>Wed, 11 Jan 2012 14:55:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Engrish in Mozambique</title>
		<link>http://www.websofia.com/2012/01/engrish-in-mozambique/</link>
		<comments>http://www.websofia.com/2012/01/engrish-in-mozambique/#comments</comments>
		<pubDate>Wed, 11 Jan 2012 14:55:07 +0000</pubDate>
		<dc:creator>alex</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.websofia.com/?p=244</guid>
		<description><![CDATA[I live in Mozambique (though I was born in Europe), and I&#8217;d like to share a little bit of this country with you in the form of our local Engrish, for lack of a better term. Sometimes business try to &#8230;<p class="read-more"><a href="http://www.websofia.com/2012/01/engrish-in-mozambique/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>I live in Mozambique (though I was born in Europe), and I&#8217;d like to share a little bit of this country with you in the form of our local Engrish, for lack of a better term. Sometimes business try to communicate to people whose first language isn&#8217;t Portuguese, which is the official language here. The result might be called Menglish, maybe, but I&#8217;ll just go with Engrish here. What follows is a list of fun stuff.</p>
<h2>Fun at the Water Park</h2>
<p>The local &#8220;Adil&#8221; water park shares some goodness:</p>
<blockquote><p>&#8220;We will not tolerate any offense against public morals, ethics and lack of bad behavior on the grounds of the park.&#8221;</p></blockquote>
<p>That&#8217;s right. ethics are good, but bad behavior is better.</p>
<h2>Resumé gems</h2>
<p>Excerpt from a great cover letter (sent to 100+ organizations &#8211; To, not BCC):</p>
<blockquote><p>Waiting for Your Excellency the greatest consideration and care to nurture more professional experience, hereby express their willingness to join in your workgroup.</p></blockquote>
<p>Naturally closely followed by this, in case there were any lingering doubts:</p>
<blockquote><p>English: Spoken and written fluently.</p></blockquote>
<p>I&#8217;ll be adding more as I dig it up&#8230;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.websofia.com/2012/01/engrish-in-mozambique/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Writing your own bootloader for a toy operating system (2)</title>
		<link>http://www.websofia.com/2011/10/writing-your-own-bootloader-for-a-toy-operating-system-2/</link>
		<comments>http://www.websofia.com/2011/10/writing-your-own-bootloader-for-a-toy-operating-system-2/#comments</comments>
		<pubDate>Fri, 21 Oct 2011 14:46:12 +0000</pubDate>
		<dc:creator>alex</dc:creator>
				<category><![CDATA[Operating System Development]]></category>
		<category><![CDATA[boot sector]]></category>
		<category><![CDATA[bootloader]]></category>
		<category><![CDATA[BPB]]></category>
		<category><![CDATA[EBPB]]></category>
		<category><![CDATA[GNU as]]></category>
		<category><![CDATA[int 0x10]]></category>
		<category><![CDATA[int 0x13]]></category>
		<category><![CDATA[int 0x16]]></category>
		<category><![CDATA[Intel]]></category>
		<category><![CDATA[toy operating system]]></category>

		<guid isPermaLink="false">http://www.websofia.com/?p=199</guid>
		<description><![CDATA[Now that we know the structure of the boot parameter block (BPB) and extended boot parameter block (EBPB), we can start writing our first code. (If you need a refresher, have a look at part 1 of this article). First &#8230;<p class="read-more"><a href="http://www.websofia.com/2011/10/writing-your-own-bootloader-for-a-toy-operating-system-2/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>Now that we know the structure of the boot parameter block (BPB) and extended boot parameter block (EBPB), we can start writing our first code. (If you need a refresher, have a look at <a title="Writing your own boot loader for a toy operating system (1)" href="http://www.websofia.com/2011/10/writing-your-own-boot-loader-for-a-toy-operating-system-1/">part 1</a> of this article).</p>
<h2>First code in GNU assembler</h2>
<p>We&#8217;ll be using the GNU assembler, since it&#8217;s free, comes with a boatload of options, supports AT&amp;T and Intel assembly syntax and plays nice with gcc and ld later on. Some of the preprocessor directives used may need some explanation, but all code will be in straightforward Intel syntax.</p>
<p>Here&#8217;s some boilerplate code to get started:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="asm"><pre class="de1"><span class="sy0">.</span>code16
<span class="sy0">.</span>intel_syntax noprefix
<span class="sy0">.</span>text
<span class="sy0">.</span><span class="kw4">org</span> <span class="nu0">0x0</span>
&nbsp;
LOAD_SEGMENT = <span class="nu0">0x1000</span>
&nbsp;
<span class="sy0">.</span><span class="kw4">global</span> main
main<span class="sy0">:</span>
  <span class="kw1">jmp</span> <span class="kw4">short</span> start
  <span class="kw1">nop</span>
&nbsp;
<span class="sy0">//</span> BPB <span class="kw1">and</span> EBPB here
&nbsp;
start<span class="sy0">:</span>
  <span class="sy0">//</span> rest of <span class="kw4">code</span></pre></div></div></div></div></div></div></div>


<p>The pile of preprocessor instructions at the top tell the assembler to assemble code for real mode. Since all (intel-based) computers start up in real mode with 16-bit instructions, we won&#8217;t be able to write 32-bit code here. We also instruct GNU as that we&#8217;ll be using Intel syntax (e.g. <em>mov ax, 1</em> instead of <em>movw $1, %ax</em> &#8211; some prefer the latter, but most readers of this text will be familiar with Intel).&nbsp; The origin of our code will be 0&#215;0, i.e. all absolute addresses start at 0&#215;0, which will be convenient.</p>
<p>Then there&#8217;s the main entry point of our code, which corresponds to the first byte of actual output when assembled. The code under &#8220;main&#8221; simply jumps over the BPB and EBPB located at offset 0&#215;3, resuming execution at the label <em>start</em>.</p>
<p>We&#8217;ve also defined a constant LOAD_SEGMENT, which is the segment where we&#8217;ll be loading our second stage bootloader (more about that later).</p>
<h2>The Boot Parameter Block</h2>
<p>The structure of the boot parameter block can be coded like this:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="asm"><pre class="de1">bootsector<span class="sy0">:</span>
 iOEM<span class="sy0">:</span>        <span class="sy0">.</span>ascii <span class="st0">&quot;DevOS   &quot;</span>  <span class="co1">; OEM String</span>
 iSectSize<span class="sy0">:</span>   <span class="sy0">.</span><span class="kw5">word</span>  <span class="nu0">0x200</span>       <span class="co1">; Bytes per sector</span>
 iClustSize<span class="sy0">:</span>  <span class="sy0">.</span><span class="kw5">byte</span>  <span class="nu0">1</span>           <span class="co1">; Sectors per cluster</span>
 iResSect<span class="sy0">:</span>    <span class="sy0">.</span><span class="kw5">word</span>  <span class="nu0">1</span>           <span class="co1">; #of reserved sectors</span>
 iFatCnt<span class="sy0">:</span>     <span class="sy0">.</span><span class="kw5">byte</span>  <span class="nu0">2</span>           <span class="co1">; #of fat copies</span>
 iRootSize<span class="sy0">:</span>   <span class="sy0">.</span><span class="kw5">word</span>  <span class="nu0">224</span>         <span class="co1">; size of root directory</span>
 iTotalSect<span class="sy0">:</span>  <span class="sy0">.</span><span class="kw5">word</span>  <span class="nu0">2880</span>        <span class="co1">; total # of sectors if &lt; 32 MB</span>
 iMedia<span class="sy0">:</span>      <span class="sy0">.</span><span class="kw5">byte</span>  <span class="nu0">0xF0</span>        <span class="co1">; Media Descriptor</span>
 iFatSize<span class="sy0">:</span>    <span class="sy0">.</span><span class="kw5">word</span>  <span class="nu0">9</span>           <span class="co1">; Size of each FAT</span>
 iTrackSect<span class="sy0">:</span>  <span class="sy0">.</span><span class="kw5">word</span>  <span class="nu0">9</span>           <span class="co1">; Sectors per track</span>
 iHeadCnt<span class="sy0">:</span>    <span class="sy0">.</span><span class="kw5">word</span>  <span class="nu0">2</span>           <span class="co1">; number of read-write heads</span>
 iHiddenSect<span class="sy0">:</span> <span class="sy0">.</span><span class="kw1">int</span>   <span class="nu0">0</span>           <span class="co1">; number of hidden sectors</span>
 iSect32<span class="sy0">:</span>     <span class="sy0">.</span><span class="kw1">int</span>   <span class="nu0">0</span>           <span class="co1">; # sectors for &gt; 32 MB</span>
 iBootDrive<span class="sy0">:</span>  <span class="sy0">.</span><span class="kw5">byte</span>  <span class="nu0">0</span>           <span class="co1">; holds drive that the boot sector came from</span>
 iReserved<span class="sy0">:</span>   <span class="sy0">.</span><span class="kw5">byte</span>  <span class="nu0">0</span>           <span class="co1">; reserved, empty</span>
 iBootSign<span class="sy0">:</span>   <span class="sy0">.</span><span class="kw5">byte</span>  <span class="nu0">0x29</span>        <span class="co1">; extended boot sector signature</span>
 iVolID<span class="sy0">:</span>      <span class="sy0">.</span>ascii <span class="st0">&quot;seri&quot;</span>      <span class="co1">; disk serial</span>
 acVolumeLabel<span class="sy0">:</span>                  <span class="co1">; just placeholder. We don't yet use volume labels.</span>
 root_strt<span class="sy0">:</span>   <span class="sy0">.</span><span class="kw5">byte</span> <span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span>          <span class="co1">; hold offset of root directory on disk</span>
 root_scts<span class="sy0">:</span>   <span class="sy0">.</span><span class="kw5">byte</span> <span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span>          <span class="co1">; holds # sectors in root directory</span>
 file_strt<span class="sy0">:</span>   <span class="sy0">.</span><span class="kw5">byte</span> <span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span>          <span class="co1">; holds offset of bootloader on disk</span>
 file_scts<span class="sy0">:</span>   <span class="sy0">.</span><span class="kw5">byte</span> <span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span>          <span class="co1">; holds # sectors in boot loader</span>
              <span class="sy0">.</span><span class="kw5">byte</span> <span class="nu0">0</span><span class="sy0">,</span><span class="nu0">0</span>
 rs_fail<span class="sy0">:</span>     <span class="sy0">.</span><span class="kw5">byte</span> <span class="nu0">0</span>            <span class="co1">; hold # tries done when attempting to read a sector</span>
 acFSType<span class="sy0">:</span>    <span class="sy0">.</span>ascii <span class="st0">&quot;FAT16   &quot;</span>  <span class="co1">; file system type</span></pre></div></div></div></div></div></div></div>


<p>The fields in this structure correspond to the specification in <a title="Writing your own boot loader for a toy operating system (1)" href="http://www.websofia.com/2011/10/writing-your-own-boot-loader-for-a-toy-operating-system-1/">part 1</a> of this text, and since they&#8217;re nicely labelled, we&#8217;ll be able to refer to them later on. Note that since we don&#8217;t volume labels here, we&#8217;re able to take the 11 bytes used for the volume label and store other things there &#8211; fields to be used later. Note that you are not required to do this: I thought it would be a great way to save space, but I later found that my bootloader did everything I wanted and I still have about 20 bytes to spare, so I could move these fields out of the EBPB after all. But for now, we&#8217;ll keep them here.</p>
<h2>Real-mode Segments</h2>
<p>After the start label, we can write some actual code. Let&#8217;s start by defining our real mode data segments:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="asm"><pre class="de1">  <span class="kw1">cli</span>                       
  <span class="kw1">mov</span>  iBootDrive<span class="sy0">,</span> <span class="kw3">dl</span>  <span class="co1">; save what drive we booted from (should be 0x0)</span>
  <span class="kw1">mov</span>  <span class="kw3">ax</span><span class="sy0">,</span> <span class="kw3">cs</span>          <span class="co1">; CS is set to 0x0, because that is where boot sector is loaded (0:07c00)</span>
  <span class="kw1">mov</span>  <span class="kw3">ds</span><span class="sy0">,</span> <span class="kw3">ax</span>          <span class="co1">; DS = CS = 0x0</span>
  <span class="kw1">mov</span>  <span class="kw3">es</span><span class="sy0">,</span> <span class="kw3">ax</span>          <span class="co1">; ES = CS = 0x0</span>
  <span class="kw1">mov</span>  <span class="kw3">ss</span><span class="sy0">,</span> <span class="kw3">ax</span>          <span class="co1">; SS = CS = 0x0</span>
  <span class="kw1">mov</span>  <span class="kw3">sp</span><span class="sy0">,</span> <span class="nu0">0x7C00</span>      <span class="co1">; Stack grows down from offset 0x7C00 toward 0x0000.</span>
  <span class="kw1">sti</span></pre></div></div></div></div></div></div></div>


<p>Here, we mask interrupts so that interrupt calls don&#8217;t mess up our sector declarations. We set ES = DS = SS = CS = 0&#215;0, and make the stack grow down from 0x7C00 (our boot loader was loaded at 0x7C00). When done, we turn the interrupts back on. It&#8217;s important to note that the BIOS places the number of the boot drive in the DL register. We store it in our BPB for later use.</p>
<h2>Resetting the disk system</h2>
<p>Next, we need to prepare the floppy drive for use. This is done through BIOS interrupt 0&#215;13, subfunction 0. We call it with the boot drive in DL:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="asm"><pre class="de1">  <span class="kw1">mov</span>  <span class="kw3">dl</span><span class="sy0">,</span> iBootDrive   <span class="co1">; drive to reset</span>
  <span class="kw1">xor</span>  <span class="kw3">ax</span><span class="sy0">,</span> <span class="kw3">ax</span>           <span class="co1">; subfunction 0</span>
  <span class="kw1">int</span>  <span class="nu0">0x13</span>             <span class="co1">; call interrupt 13h</span>
  <span class="kw1">jc</span>   bootFailure      <span class="co1">; display error message if carry set (error)</span></pre></div></div></div></div></div></div></div>


<p>If the reset fails, the carry flag will be set and we jump to a label where we handle a boot failure by showing a message, waiting for a keypress and rebooting. Come to think of it, we&#8217;ll need a way to print a string to the screen.</p>
<h2>Printing a string</h2>
<p>We&#8217;ll add a short function that uses BIOS interrupt 0&#215;10, subfunction 9 to print characters to the screen. The calling code must point DS:SI to the null-terminated string to be printed.</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="asm"><pre class="de1"><span class="sy0">.</span>func WriteString
 WriteString<span class="sy0">:</span>
  <span class="kw1">lodsb</span>                   <span class="co1">; load byte at ds:si into al (advancing si)</span>
  <span class="kw1">or</span>     <span class="kw3">al</span><span class="sy0">,</span> <span class="kw3">al</span>           <span class="co1">; test if character is 0 (end)</span>
  <span class="kw1">jz</span>     WriteString_done <span class="co1">; jump to end if 0.</span>
&nbsp;
  <span class="kw1">mov</span>    <span class="kw3">ah</span><span class="sy0">,</span> <span class="nu0">0xe</span>          <span class="co1">; Subfunction 0xe of int 10h (video teletype output).</span>
  <span class="kw1">mov</span>    <span class="kw3">bx</span><span class="sy0">,</span> <span class="nu0">9</span>            <span class="co1">; Set bh (page number) to 0, and bl (attribute) to white (9).</span>
  <span class="kw1">int</span>    <span class="nu0">0x10</span>             <span class="co1">; call BIOS interrupt.</span>
&nbsp;
  <span class="kw1">jmp</span>    WriteString      <span class="co1">; Repeat for next character.</span>
&nbsp;
 WriteString_done<span class="sy0">:</span>
  retw
<span class="sy0">.</span>endfunc</pre></div></div></div></div></div></div></div>


<p>We can now define the &#8220;bootFailure&#8221; label:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="asm"><pre class="de1">diskerror<span class="sy0">:</span> <span class="sy0">.</span>asciz <span class="st0">&quot;Disk error. &quot;</span>
bootFailure<span class="sy0">:</span>
  <span class="kw1">lea</span> <span class="kw3">si</span><span class="sy0">,</span> diskerror
  <span class="kw1">call</span> WriteString
  <span class="kw1">call</span> Reboot</pre></div></div></div></div></div></div></div>


<p>Great. We&#8217;ve got code to reset the floppy drive, and if it fails, there&#8217;s code that prints failure strings and reboots. Although, we still have to write a Reboot function.</p>
<h2>Rebooting</h2>
<p>Here is some code that prints a &#8220;Press any key to reboot&#8221; message, waits for a keystroke, and reboots the machine.</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="asm"><pre class="de1">rebootmsg<span class="sy0">:</span> <span class="sy0">.</span>asciz <span class="st0">&quot;Press any key to reboot\r\n&quot;</span>
<span class="sy0">.</span>func Reboot
 Reboot<span class="sy0">:</span>
  <span class="kw1">lea</span>    <span class="kw3">si</span><span class="sy0">,</span> rebootmsg    <span class="co1">; Load address of reboot message into si</span>
  <span class="kw1">call</span>   WriteString      <span class="co1">; print the string</span>
  <span class="kw1">xor</span>    <span class="kw3">ax</span><span class="sy0">,</span> <span class="kw3">ax</span>           <span class="co1">; subfuction 0</span>
  <span class="kw1">int</span>    <span class="nu0">0x16</span>             <span class="co1">; call bios to wait for key</span>
&nbsp;
  <span class="sy0">.</span><span class="kw5">byte</span>  <span class="nu0">0xEA</span>             <span class="co1">; machine language to jump to FFFF:0000 (reboot)</span>
  <span class="sy0">.</span><span class="kw5">word</span>  <span class="nu0">0x0000</span>
  <span class="sy0">.</span><span class="kw5">word</span>  <span class="nu0">0xFFFF</span>
<span class="sy0">.</span>endfunc</pre></div></div></div></div></div></div></div>


<p>Here, we use BIOS interrupt 0&#215;16, subfunction to read a key (any key). We then add a far jump to 0xffff:0000 we causes the machine to reboot.</p>
<h2>Summary</h2>
<p>We&#8217;ve written assembler code that prepares data and stack segments and resets the floppy drive. We&#8217;ve also added functions for writing text to the screen, waiting for a keypress, and rebooting, which wraps up most of the framework we&#8217;ll need for the rest of the bootloader. In the next section, we&#8217;ll write code that actually accesses the floppy drive to load our kernel (or second stage bootloader, actually, but we&#8217;ll sort that out).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.websofia.com/2011/10/writing-your-own-bootloader-for-a-toy-operating-system-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Writing your own boot loader for a toy operating system (1)</title>
		<link>http://www.websofia.com/2011/10/writing-your-own-boot-loader-for-a-toy-operating-system-1/</link>
		<comments>http://www.websofia.com/2011/10/writing-your-own-boot-loader-for-a-toy-operating-system-1/#comments</comments>
		<pubDate>Fri, 21 Oct 2011 13:47:12 +0000</pubDate>
		<dc:creator>alex</dc:creator>
				<category><![CDATA[Operating System Development]]></category>
		<category><![CDATA[assembler]]></category>
		<category><![CDATA[boot sector]]></category>
		<category><![CDATA[bootloader]]></category>
		<category><![CDATA[BPB]]></category>
		<category><![CDATA[DOS]]></category>
		<category><![CDATA[EBPB]]></category>
		<category><![CDATA[FAT]]></category>
		<category><![CDATA[FAT16]]></category>
		<category><![CDATA[GRUB]]></category>
		<category><![CDATA[IO.SYS]]></category>
		<category><![CDATA[LILO]]></category>
		<category><![CDATA[SYS.COM]]></category>

		<guid isPermaLink="false">http://www.websofia.com/?p=180</guid>
		<description><![CDATA[If you&#8217;re writing your own toy operating system, the first thing you&#8217;ll need is a boot sector. It&#8217;s a piece of code that lives in the first sector of a (floppy) disk. This code gets called by the BIOS as &#8230;<p class="read-more"><a href="http://www.websofia.com/2011/10/writing-your-own-boot-loader-for-a-toy-operating-system-1/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re writing your own toy operating system, the first thing you&#8217;ll need is a <strong>boot sector</strong>. It&#8217;s a piece of code that lives in the first sector of a (floppy) disk. This code gets called by the BIOS as soon as the computer starts up.</p>
<p>Do note that you can actually start developing other components of your toy operating system before writing boot code, since you can use <a title="GRUB" href="http://www.gnu.org/software/grub/">GRUB</a> (GNU Grand Unified Boot Loader) or <a title="LILO" href="http://freshmeat.net/projects/lilo/">LILO</a> to start your kernel. Using one of these tools brings advantages, since they&#8217;ll switch the processor to protected mode for you, and allow you to load kernels that are placed beyond cylinder 1024 of a hard disk.</p>
<p>However, writing your own boot code can be a very interesting exercise in assembly programming, and you&#8217;ll have full control over what your bootloader actually does. Plus, you get to try and do it better than the people who wrote the DOS/Win95 bootloaders (which isn&#8217;t saying a lot as you&#8217;ll see below).</p>
<h2>Boot loader requirements</h2>
<p>The boot code lives in the first sector of a floppy disk, which typically has a size of 512 bytes. However, 61 of those bytes are occupied by data, placed on the disk when it is formatted. This data includes the size of a disk sector, number of FAT tables, number of tracks per sector, volume ID, and more. This yields 451 bytes available for code, which is not a whole lot. That&#8217;s one reason we&#8217;ll use assembler to write our code.</p>
<h2>The DOS/Windows bootloader and its limitations</h2>
<p>Let&#8217;s consider the bootloader that most of us have used many times: the bootloader that comes with DOS or Windows (up to Windows 95). What does it do?</p>
<ul>
<li>Reset the floppy disk system</li>
<li>Read the first sector of the root directory from the disk</li>
<li>Verify that the first file found there is IO.SYS (the kernel)</li>
<li>Load IO.SYS  into memory</li>
<li>Transfer control to IO.SYS</li>
</ul>
<p>Since the space available for actual code in the boot sector is limited, the author of the DOS bootloader introduced an important requirement: the file IO.SYS must be the first file in the root directory. The DOS code does not scan the entire root directory looking for the required file. If IO.SYS is not the first file found, then the boot code fails.</p>
<p>This is why DOS/Windows come with the SYS.COM program, which is used to make a disk bootable. This program actually cleans the root directory of a floppy disk and copies IO.SYS into it as the first entry, effectively removing all the other files. It would have been much nicer if it had been possible to copy IO.SYS to the root directory of a disk, in <em>any</em> position. Then any disk could be make bootable without sacrificing the files on it. This can actually be done, but it requires more assembly code, something the DOS developers apparently did not find any space for &#8211; but we can do better.</p>
<p>At any rate, modern operating systems will switch the processor to protected mode, which allows us to address up to 4 GB of memory in a flat model, and switch on paging to protect processes from one another. This wasn&#8217;t part of DOS/Windows 95, but we&#8217;ll need to do it.</p>
<h2>How a boot loader gets called</h2>
<p>When the computer starts up, it executes a power-on self test (POST). It then performs the following actions:</p>
<ul>
<li>Determine which device (drive) to use for booting, using preferences stored in the CMOS.</li>
<li>Try to load the first sector (and only the first sector) from the boot drive into memory at address 0:0x7C00.</li>
<li>Verify that the the first sector is in fact bootable by checking for the presence of a magic number (see below).</li>
<li>Store the number of the drive used in register DL.</li>
<li>Point the CPU&#8217;s instruction pointer to 0:0x7C00, and start execution from there.</li>
</ul>
<h2>What a boot loader should do</h2>
<p>Here&#8217;s a list of things that a bootloader should do in order to load your operating system&#8217;s kernel (we&#8217;ll cover concepts like the A20-line, IDT and GDT tables later):</p>
<ul>
<li>Reset the floppy disk system</li>
<li>Write a &#8220;loading&#8221; message to the screen</li>
<li>Find the kernel in the root directory of the disk (at any position)</li>
<li>Read the kernel from disk into memory</li>
<li>Enable the A20-line</li>
<li>Setup the IDT and GDT tables</li>
<li>Switch to protected mode</li>
<li>Clear the processor prefetch queue</li>
<li>Run the kernel</li>
</ul>
<h2>Boot Sector Layout</h2>
<p>The boot sector of a floppy disk has a very specific layout, because the BIOS requires access to certain data which it needs to find in the place it expects it to be. Also, an operating system will need to access this data to determine how large the disk is, what file system it uses, what its volume label is and so on. For this article, we&#8217;ll assume a floppy disk formatted with a FAT16 file system. The layout of the boot sector is then:</p>
<p><strong>
<table id="wp-table-reloaded-id-1-no-1" class="wp-table-reloaded wp-table-reloaded-id-1">
<thead>
	<tr class="row-1 odd">
		<th class="column-1">Offset</th><th class="column-2">Size</th><th class="column-3"></th><th class="column-4">Contents</th><th class="column-5">Typical value</th>
	</tr>
</thead>
<tbody class="row-hover">
	<tr class="row-2 even">
		<td class="column-1">0000</td><td class="column-2">3</td><td class="column-3">Code</td><td class="column-4">Jump to rest of code</td><td class="column-5"></td>
	</tr>
	<tr class="row-3 odd">
		<td class="column-1">0003</td><td class="column-2">8</td><td rowspan="12" class="column-3 rowspan-12">BPB</td><td class="column-4">OEM name</td><td class="column-5">Great-OS</td>
	</tr>
	<tr class="row-4 even">
		<td class="column-1">0011</td><td class="column-2">2</td><td class="column-4">Bytes per sector</td><td class="column-5">512</td>
	</tr>
	<tr class="row-5 odd">
		<td class="column-1">0013</td><td class="column-2">1</td><td class="column-4">Number of sectors per cluster</td><td class="column-5">1</td>
	</tr>
	<tr class="row-6 even">
		<td class="column-1">0014</td><td class="column-2">2</td><td class="column-4">Number of reserved sectors</td><td class="column-5">1</td>
	</tr>
	<tr class="row-7 odd">
		<td class="column-1">0016</td><td class="column-2">1</td><td class="column-4">Number of FAT tables</td><td class="column-5">2</td>
	</tr>
	<tr class="row-8 even">
		<td class="column-1">0017</td><td class="column-2">2</td><td class="column-4">Number of root directory entries (usually 224)</td><td class="column-5">224</td>
	</tr>
	<tr class="row-9 odd">
		<td class="column-1">0019</td><td class="column-2">2</td><td class="column-4">Total number of sectors</td><td class="column-5">2880</td>
	</tr>
	<tr class="row-10 even">
		<td class="column-1">0021</td><td class="column-2">1</td><td class="column-4">Media descriptor</td><td class="column-5">0xf0</td>
	</tr>
	<tr class="row-11 odd">
		<td class="column-1">0022</td><td class="column-2">2</td><td class="column-4">Number of sectors per FAT</td><td class="column-5">9</td>
	</tr>
	<tr class="row-12 even">
		<td class="column-1">0024</td><td class="column-2">2</td><td class="column-4">Number of sectors/track</td><td class="column-5">9</td>
	</tr>
	<tr class="row-13 odd">
		<td class="column-1">0026</td><td class="column-2">2</td><td class="column-4">Number of heads</td><td class="column-5">2</td>
	</tr>
	<tr class="row-14 even">
		<td class="column-1">0028</td><td class="column-2">2</td><td class="column-4">Number of hidden sectors</td><td class="column-5">0</td>
	</tr>
	<tr class="row-15 odd">
		<td class="column-1">0030</td><td class="column-2">2</td><td rowspan="8" class="column-3 rowspan-8">EBPB</td><td class="column-4">Number of hidden sectors (high word)</td><td class="column-5">0</td>
	</tr>
	<tr class="row-16 even">
		<td class="column-1">0032</td><td class="column-2">4</td><td class="column-4">Total number of sectors in filesystem</td><td class="column-5"></td>
	</tr>
	<tr class="row-17 odd">
		<td class="column-1">0036</td><td class="column-2">1</td><td class="column-4">Logical drive number</td><td class="column-5">0</td>
	</tr>
	<tr class="row-18 even">
		<td class="column-1">0037</td><td class="column-2">1</td><td class="column-4">Reserved</td><td class="column-5"></td>
	</tr>
	<tr class="row-19 odd">
		<td class="column-1">0038</td><td class="column-2">1</td><td class="column-4">Extended signature</td><td class="column-5">0x29</td>
	</tr>
	<tr class="row-20 even">
		<td class="column-1">0039</td><td class="column-2">4</td><td class="column-4">Serial number</td><td class="column-5"></td>
	</tr>
	<tr class="row-21 odd">
		<td class="column-1">0043</td><td class="column-2">8</td><td class="column-4">Volume label</td><td class="column-5">MYVOLUME</td>
	</tr>
	<tr class="row-22 even">
		<td class="column-1">0054</td><td class="column-2">8</td><td class="column-4">Filesystem type</td><td class="column-5">FAT16   </td>
	</tr>
	<tr class="row-23 odd">
		<td class="column-1">0062</td><td class="column-2">448</td><td class="column-3">Code</td><td class="column-4">Boot code</td><td class="column-5"></td>
	</tr>
	<tr class="row-24 even">
		<td class="column-1">0510</td><td class="column-2">2</td><td class="column-3">Required</td><td class="column-4">Boot signature</td><td class="column-5">0xaa55</td>
	</tr>
</tbody>
</table>
</strong></p>
<p>A required element of the boot sector is the boot parameter block (BPB) and the extended boot parameter block (EBPB, for FAT16). This block must be placed at offset 3, size 59 bytes. Also, the boot sector must end with the magic number 0xaa55: some BIOSes will check whether this value is present at offset 510. If not, the BIOS will refuse to boot from the disk. All other bytes are available for us to fill in. We can calculate that that adds in fact up to 451 bytes. Also, the first three bytes are separated from the rest and should only be used to jump to the rest of the code, so that&#8217;s less 3 bytes for interesting code&#8230;</p>
<p>Here is a typical hex dump of a boot sector without any code. Colored in red are the parts in the BPB and EBPB as decribed above, and the magic number at the end. Everything else is available for code:</p>
<pre>0x0000 00 00 00<span style="color: #993300;"> 47 72 65 61 74 2d 4f 53 00 02 01 01 00</span> ...<span style="color: #993300;">Great-OS.....</span>
0x0010 <span style="color: #993300;">02 e0 00 40 0b f0 09 00 09 00 02 00 00 00 00 00 .à.@............</span>
0x0020 <span style="color: #993300;">00 00 00 00 00 00 29 73 65 72 69 00 00 00 00 00 ......)seri.....</span>
0x0030 <span style="color: #993300;">00 00 00 00 00 00 46 41 54 31 36 20 20 20 fa 88 ......FAT16   ú^</span>
0x0040 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0050 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0060 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0070 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0080 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0090 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x00a0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x00b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x00c0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x00d0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x00e0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x00f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0100 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0110 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0120 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0130 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0140 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0150 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0160 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0170 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0180 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0190 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x01a0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x01b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x01c0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x01d0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x01e0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x01f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 <span style="color: #993300;">55 aa</span> ..............<span style="color: #993300;">Uª</span></pre>
<p>This article continues in <a title="Writing your own bootloader for a toy operating system (2)" href="http://www.websofia.com/2011/10/writing-your-own-bootloader-for-a-toy-operating-system-2/">part 2</a> of this series.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.websofia.com/2011/10/writing-your-own-boot-loader-for-a-toy-operating-system-1/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Jiskefet Lullo&#8217;s Gezinsverpakking: alle video&#8217;s!</title>
		<link>http://www.websofia.com/2011/10/jiskefet-lullos-gezinsverpakking-alle-videos/</link>
		<comments>http://www.websofia.com/2011/10/jiskefet-lullos-gezinsverpakking-alle-videos/#comments</comments>
		<pubDate>Thu, 20 Oct 2011 12:26:22 +0000</pubDate>
		<dc:creator>alex</dc:creator>
				<category><![CDATA[Jiskefet]]></category>
		<category><![CDATA[dispuut]]></category>
		<category><![CDATA[Jisketfet]]></category>
		<category><![CDATA[kamphuys]]></category>
		<category><![CDATA[kerstens]]></category>
		<category><![CDATA[Lullo's]]></category>
		<category><![CDATA[pizza]]></category>
		<category><![CDATA[skybox]]></category>
		<category><![CDATA[van binsbergen]]></category>

		<guid isPermaLink="false">http://www.websofia.com/?p=173</guid>
		<description><![CDATA[Hier zijn ze dan:]]></description>
			<content:encoded><![CDATA[<p>Hier zijn ze dan:</p>
<p><iframe src="http://www.youtube.com/embed/Jgd8g3oqZus" width="420" frameborder="0" height="315"></iframe></p>
<p><iframe width="420" height="315" src="http://www.youtube.com/embed/6tlOPm_RFHI" frameborder="0" allowfullscreen></iframe></p>
<p><iframe width="420" height="315" src="http://www.youtube.com/embed/ZbnKqA3rjOU" frameborder="0" allowfullscreen></iframe></p>
<p><iframe width="420" height="315" src="http://www.youtube.com/embed/6UQvdN7RkQI" frameborder="0" allowfullscreen></iframe></p>
<p><iframe width="420" height="315" src="http://www.youtube.com/embed/kIJO584aX0o" frameborder="0" allowfullscreen></iframe></p>
<p><iframe width="420" height="315" src="http://www.youtube.com/embed/fdEFNdyIGwg" frameborder="0" allowfullscreen></iframe></p>
<p><iframe width="420" height="315" src="http://www.youtube.com/embed/7Uc6jIruIBM" frameborder="0" allowfullscreen></iframe></p>
<p><iframe width="420" height="315" src="http://www.youtube.com/embed/B723xCdnvZs" frameborder="0" allowfullscreen></iframe></p>
<p><iframe width="420" height="315" src="http://www.youtube.com/embed/XK0K04VHTrM" frameborder="0" allowfullscreen></iframe></p>
<p><iframe width="420" height="315" src="http://www.youtube.com/embed/lh3lsTu7W9A" frameborder="0" allowfullscreen></iframe></p>
<p><iframe width="420" height="315" src="http://www.youtube.com/embed/ASqvFdANCJg" frameborder="0" allowfullscreen></iframe></p>
<p><iframe width="420" height="315" src="http://www.youtube.com/embed/xv0CivbC3TY" frameborder="0" allowfullscreen></iframe></p>
<p><iframe width="420" height="315" src="http://www.youtube.com/embed/JUijaUl04-Q" frameborder="0" allowfullscreen></iframe></p>
]]></content:encoded>
			<wfw:commentRss>http://www.websofia.com/2011/10/jiskefet-lullos-gezinsverpakking-alle-videos/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Linking a flat binary from C with MinGW</title>
		<link>http://www.websofia.com/2011/10/linking-a-flat-binary-from-c-with-mingw/</link>
		<comments>http://www.websofia.com/2011/10/linking-a-flat-binary-from-c-with-mingw/#comments</comments>
		<pubDate>Thu, 20 Oct 2011 10:17:42 +0000</pubDate>
		<dc:creator>alex</dc:creator>
				<category><![CDATA[Operating System Development]]></category>
		<category><![CDATA[asm]]></category>
		<category><![CDATA[assembler]]></category>
		<category><![CDATA[boot sector]]></category>
		<category><![CDATA[flat binary]]></category>
		<category><![CDATA[gcc]]></category>
		<category><![CDATA[kernel]]></category>
		<category><![CDATA[ld]]></category>
		<category><![CDATA[MinGW]]></category>
		<category><![CDATA[nostdlib]]></category>
		<category><![CDATA[objcopy]]></category>
		<category><![CDATA[objdump]]></category>
		<category><![CDATA[operating system]]></category>
		<category><![CDATA[PE header]]></category>
		<category><![CDATA[pedantic-errors]]></category>
		<category><![CDATA[__main]]></category>
		<category><![CDATA[___main]]></category>

		<guid isPermaLink="false">http://www.websofia.com/?p=153</guid>
		<description><![CDATA[If you&#8217;re trying to compile a kernel written in C for your own toy operating system, you may run into trouble compiling/linking your code. Assuming you&#8217;re using GRUB to load your kernel, or you&#8217;ve rolled your own boot sector, you&#8217;ll &#8230;<p class="read-more"><a href="http://www.websofia.com/2011/10/linking-a-flat-binary-from-c-with-mingw/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re trying to compile a kernel written in C for your own toy operating system, you may run into trouble compiling/linking your code. Assuming you&#8217;re using GRUB to load your kernel, or you&#8217;ve rolled your own boot sector, you&#8217;ll now want to compile your kernel code (written in C) to a flat binary. The toolchain provided by MinGW (gcc and ld) is well suited for this, as long as you know a few tricks.</p>
<p>Let&#8217;s start with a very simple kernel.c program just to see if we can get things working:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="c"><pre class="de1"><span class="kw4">int</span> main<span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>
<span class="br0">&#123;</span>
mylabel<span class="sy0">:</span>
  <span class="kw1">goto</span> mylabel<span class="sy0">;</span>
<span class="br0">&#125;</span></pre></div></div></div></div></div></div></div>


<p>We&#8217;ll compile this with gcc, switching on all warnings (the compiler is our friend):</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="bash"><pre class="de1"><span class="kw2">gcc</span> <span class="re5">-Wall</span> <span class="re5">-pedantic-errors</span> kernel.c <span class="re5">-o</span> kernel.exe</pre></div></div></div></div></div></div></div>


<p>This will yield a working program that we can actually execute at the command prompt. It&#8217;ll pause indefinitely, as desired. However, there are a number of problems with the resulting binary:</p>
<p>First, the binary includes a PE header, which specifies how Windows must load and execute the program. We&#8217;re writing a kernel, so we don&#8217;t want any of this header data. We must find it way to remove it.</p>
<p>Second, the program is relocatable. The operating system (i.e. Windows) will load the code into memory where it wants, then use the information contained in the PE header to make sure that all references are correct. The references are provided relatively, that is, the can be relocated. For our kernel, this is not what we want: we want to load our kernel at a specific address (say 0&#215;20000) and make all references work precisely (statically) there.</p>
<p>This can be illustrated by running objdump:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="bash"><pre class="de1">$ objdump <span class="re5">-f</span> kernel.exe
kernel.exe: <span class="kw2">file</span> format pei-i386
architecture: i386, flags 0x00000132:
EXEC_P, HAS_SYMS, HAS_LOCALS, D_PAGED
start address 0x00401160</pre></div></div></div></div></div></div></div>


<p>Objdump&#8217;s output shows that a PE header is present (pei-i386 file format) and that a default random start adress of 0&#215;00401160 has been defined. Let&#8217;s see what we can do about the start address. Since we want our kernel to always run at 0&#215;20000, we can instruct the linked to use that address to place the code. Linker options can be passed to gcc:</p>
<p>Hint: do not use gcc to compile but not link, then ld to do the linking separately. Strange error messages will ensue. It&#8217;s easier to simply pass the linking options to gcc and let gcc call ld for you.</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="bash"><pre class="de1">$ <span class="kw2">gcc</span> <span class="re5">-Wall</span> <span class="re5">-pedantic-errors</span> kernel.c <span class="re5">-o</span> kernel.exe -Wl,-Ttext=0x20000
$ objdump <span class="re5">-f</span> kernel.exe
kernel.exe: <span class="kw2">file</span> format pei-i386
architecture: i386, flags 0x00000132:
EXEC_P, HAS_SYMS, HAS_LOCALS, D_PAGED
start address 0x00020160</pre></div></div></div></div></div></div></div>


<p>Oh look: our start address is now 0&#215;00020160. The excess 0&#215;160 bytes are the space occupied by the header, which we don&#8217;t want. We can try to pass the option &#8211;oformat binary to the linker, which will make it link a flat binary for us. Unfortunately (under MinGW), we get this:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="bash"><pre class="de1">c:<span class="sy0">/</span>mingw<span class="sy0">/</span>bin<span class="sy0">/</span>..<span class="sy0">/</span>lib<span class="sy0">/</span>gcc<span class="sy0">/</span>mingw32<span class="sy0">/</span>4.5.2<span class="sy0">/</span>..<span class="sy0">/</span>..<span class="sy0">/</span>..<span class="sy0">/</span>..<span class="sy0">/</span>mingw32<span class="sy0">/</span>bin<span class="sy0">/</span>ld.exe:
cannot perform PE operations on non PE output <span class="kw2">file</span> <span class="st_h">'kernel.exe'</span>.
collect2: <span class="kw2">ld</span> returned <span class="nu0">1</span> <span class="kw3">exit</span> status</pre></div></div></div></div></div></div></div>


<p>This can be resolved though: let the linker create the kernel.exe executable, then pass it through objcopy to create the flat binary:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="bash"><pre class="de1">objcopy <span class="re5">-O</span> binary <span class="re5">-j</span> .text kernel.exe kernel.bin</pre></div></div></div></div></div></div></div>


<p>This will yield, finally, an executable. Unfortunately, it&#8217;s 3376 bytes in size! About 10 bytes would be closer to the mark. Obviously, code is being included that we didn&#8217;t write: references to standard libraries. Since we don&#8217;t have any standard libraries in our fledgling operating system, we&#8217;ll need to remove this. This can be done by passing the -nostdlib argument to gcc:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="bash"><pre class="de1">$ <span class="kw2">gcc</span> <span class="re5">-Wall</span> <span class="re5">-pedantic-errors</span> kernel.c <span class="re5">-o</span> kernel.exe <span class="re5">-nostdlib</span> -Wl,-Ttext=0x20000
C:\Users\Alex\AppData\Local\Temp\cc5nshHf.o:kernel.c:<span class="br0">&#40;</span>.text+0x7<span class="br0">&#41;</span>:
  undefined reference to <span class="sy0">`</span>__main<span class="st_h">'
collect2: ld returned 1 exit status</span></pre></div></div></div></div></div></div></div>


<p>Foiled again! Now that we have no standard libraries, ld is looking for startup code that doesn&#8217;t exist. We did write a main function, but it&#8217;s actually looking for a wrapper to that main function normally supplied by the standard libraries. Let&#8217;s try a different approach: we&#8217;ll rename our main function.</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="c"><pre class="de1"><span class="kw4">int</span> start<span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>
<span class="br0">&#123;</span>
mylabel<span class="sy0">:</span>
  <span class="kw1">goto</span> mylabel<span class="sy0">;</span>
<span class="br0">&#125;</span></pre></div></div></div></div></div></div></div>


<p>Now our code compiles, and we&#8217;re down to a flat binary of 2011 bytes. It turns out that we must also pass -nostdlib to the linker:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="bash"><pre class="de1">$ <span class="kw2">gcc</span> <span class="re5">-Wall</span> <span class="re5">-pedantic-errors</span> kernel.c <span class="re5">-o</span> kernel.exe <span class="re5">-nostdlib</span>
  -Wl,-Ttext=0x20000,-nostdlib</pre></div></div></div></div></div></div></div>


<p>Now we get an executable of 24 bytes. In fact, on my system I get:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="bash"><pre class="de1">00000000h: <span class="nu0">55</span> <span class="nu0">89</span> e5 eb fe <span class="nu0">90</span> <span class="nu0">90</span> <span class="nu0">90</span> ff ff ff ff 00 00 00 00
00000010h: ff ff ff ff 00 00 00 00</pre></div></div></div></div></div></div></div>


<p>When disassembled, this yields:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="asm"><pre class="de1"><span class="kw1">push</span> <span class="kw3">ebp</span>
<span class="kw1">mov</span> <span class="kw3">ebp</span><span class="sy0">,</span> <span class="kw3">esp</span>
<span class="kw1">jmp</span> <span class="sy0">.-</span><span class="nu0">2</span></pre></div></div></div></div></div></div></div>


<p>This corresponds exactly to the code we wrote: a stack frame is created for the start function (even though we are not interested in it &#8211; a C program must always start with a function), then an infinite loop is entered (which we wrote using a label and a goto statement).</p>
<p>Wait&#8230; this code only occupies 5 bytes. So why are there 24 bytes in the flat binary image? We can see that the first three unneeded bytes have a value of 0&#215;90, which corresponds to NOP instructions. This is probably added to get at least an 8-byte boundary. However, why an additional 16 bytes are added, I actually don&#8217;t know. If anyone can explain, I&#8217;d be grateful.</p>
<p>Nevertheless, we have now produced a flat binary that can be launched by our boot sector or second stage boot loader. It can be placed at 0&#215;20000 and includes no undesired headers. Just the code, please, ma&#8217;am.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.websofia.com/2011/10/linking-a-flat-binary-from-c-with-mingw/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Installing Ruby&#8217;s mysql2 gem for MySQL 64-bits</title>
		<link>http://www.websofia.com/2011/08/installing-rubys-mysql2-gem-for-mysql-64-bits/</link>
		<comments>http://www.websofia.com/2011/08/installing-rubys-mysql2-gem-for-mysql-64-bits/#comments</comments>
		<pubDate>Fri, 12 Aug 2011 11:27:41 +0000</pubDate>
		<dc:creator>alex</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[64-bits]]></category>
		<category><![CDATA[gem]]></category>
		<category><![CDATA[libymysql.dll]]></category>
		<category><![CDATA[mysql2]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://www.websofia.com/?p=150</guid>
		<description><![CDATA[I was trying to install Ruby&#8217;s mysql2 gem on my Windows 7 computer, running MySQL 64-bits. It turns out that the mysql2 gem is not compatible with MySQL 64-bits libmysql.dll file. When installing, you get something like: ﻿c:\&#62;gem install mysql2 &#8230;<p class="read-more"><a href="http://www.websofia.com/2011/08/installing-rubys-mysql2-gem-for-mysql-64-bits/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>I was trying to install Ruby&#8217;s mysql2 gem on my Windows 7 computer, running MySQL 64-bits. It turns out that the mysql2 gem is <em>not</em> compatible with MySQL 64-bits <em>libmysql.dll</em> file.</p>
<p>When installing, you get something like:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="bash"><pre class="de1">﻿c:\<span class="sy0">&gt;</span>gem <span class="kw2">install</span> mysql2 <span class="re5">--</span> with-mysql-include=x:\include
Temporarily enhancing PATH to include DevKit...
Building native extensions.  This could take a while...
ERROR:  Error installing mysql2:
ERROR: Failed to build gem native extension.
&nbsp;
C:<span class="sy0">/</span>Ruby192<span class="sy0">/</span>bin<span class="sy0">/</span>ruby.exe extconf.rb with-mysql-include=x:\include
checking <span class="kw1">for</span> rb_thread_blocking_region<span class="br0">&#40;</span><span class="br0">&#41;</span>... <span class="kw2">yes</span>
checking <span class="kw1">for</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw1">in</span> -llibmysql... <span class="kw2">yes</span>
checking <span class="kw1">for</span> mysql.h... <span class="kw2">yes</span>
checking <span class="kw1">for</span> errmsg.h... <span class="kw2">yes</span>
checking <span class="kw1">for</span> mysqld_error.h... <span class="kw2">yes</span>
creating Makefile
&nbsp;
<span class="kw2">make</span>
C:<span class="sy0">/</span>Ruby192<span class="sy0">/</span>bin<span class="sy0">/</span>ruby <span class="re5">-e</span> <span class="st0">&quot;puts 'EXPORTS', 'Init_mysql2'&quot;</span>  <span class="sy0">&gt;</span> mysql2-i386-mingw32.def
<span class="kw2">gcc</span> -I. -IC:<span class="sy0">/</span>Ruby192<span class="sy0">/</span>include<span class="sy0">/</span>ruby-1.9.1<span class="sy0">/</span>i386-mingw32 -I<span class="sy0">/</span>C<span class="sy0">/</span>Ruby192<span class="sy0">/</span>include<span class="sy0">/</span>ruby-1.9.1<span class="sy0">/</span>ruby<span class="sy0">/</span>backward -I<span class="sy0">/</span>C<span class="sy0">/</span>Ruby192<span class="sy0">/</span>include<span class="sy0">/</span>ruby-1.9.1 -I. -DHAVE_RB_THREAD_BLOCKING_REGION -DHAVE_MYSQL_H -DHAVE_ERRMSG_H -DHAVE_MYSQLD_ERROR_H -Ix:\include <span class="re5">-O3</span> <span class="re5">-g</span> <span class="re5">-Wextra</span> <span class="re5">-Wno-unused-parameter</span> <span class="re5">-Wno-parentheses</span> <span class="re5">-Wpointer-arith</span> <span class="re5">-Wwrite-strings</span> <span class="re5">-Wno-missing-field-initializers</span> <span class="re5">-Wno-long-long</span> <span class="re5">-Wall</span> <span class="re5">-funroll-loops</span>  <span class="re5">-o</span> client.o <span class="re5">-c</span> client.c
<span class="kw2">gcc</span> -I. -IC:<span class="sy0">/</span>Ruby192<span class="sy0">/</span>include<span class="sy0">/</span>ruby-1.9.1<span class="sy0">/</span>i386-mingw32 -I<span class="sy0">/</span>C<span class="sy0">/</span>Ruby192<span class="sy0">/</span>include<span class="sy0">/</span>ruby-1.9.1<span class="sy0">/</span>ruby<span class="sy0">/</span>backward -I<span class="sy0">/</span>C<span class="sy0">/</span>Ruby192<span class="sy0">/</span>include<span class="sy0">/</span>ruby-1.9.1 -I. -DHAVE_RB_THREAD_BLOCKING_REGION -DHAVE_MYSQL_H -DHAVE_ERRMSG_H -DHAVE_MYSQLD_ERROR_H -Ix:\include    <span class="re5">-O3</span> <span class="re5">-g</span> <span class="re5">-Wextra</span> <span class="re5">-Wno-unused-parameter</span> <span class="re5">-Wno-parentheses</span> <span class="re5">-Wpointer-arith</span> <span class="re5">-Wwrite-strings</span> <span class="re5">-Wno-missing-field-initializers</span> <span class="re5">-Wno-long-long</span> <span class="re5">-Wall</span> <span class="re5">-funroll-loops</span> <span class="re5">-o</span> mysql2_ext.o <span class="re5">-c</span> mysql2_ext.c
<span class="kw2">gcc</span> -I. -IC:<span class="sy0">/</span>Ruby192<span class="sy0">/</span>include<span class="sy0">/</span>ruby-1.9.1<span class="sy0">/</span>i386-mingw32 -I<span class="sy0">/</span>C<span class="sy0">/</span>Ruby192<span class="sy0">/</span>include<span class="sy0">/</span>ruby-1.9.1<span class="sy0">/</span>ruby<span class="sy0">/</span>backward -I<span class="sy0">/</span>C<span class="sy0">/</span>Ruby192<span class="sy0">/</span>include<span class="sy0">/</span>ruby-1.9.1 -I. -DHAVE_RB_THREAD_BLOCKING_REGION -DHAVE_MYSQL_H -DHAVE_ERRMSG_H -DHAVE_MYSQLD_ERROR_H -Ix:\include    <span class="re5">-O3</span> <span class="re5">-g</span> <span class="re5">-Wextra</span> <span class="re5">-Wno-unused-parameter</span> <span class="re5">-Wno-parentheses</span> <span class="re5">-Wpointer-arith</span> <span class="re5">-Wwrite-strings</span> <span class="re5">-Wno-missing-field-initializers</span> <span class="re5">-Wno-long-long</span> <span class="re5">-Wall</span> <span class="re5">-funroll-loops</span>  <span class="re5">-o</span> result.o <span class="re5">-c</span> result.c
result.c: In <span class="kw1">function</span> <span class="st_h">'rb_mysql_result_fetch_fields'</span>:
result.c:<span class="nu0">376</span>:<span class="nu0">35</span>: warning: comparison between signed and unsigned integer expressions
<span class="kw2">gcc</span> <span class="re5">-shared</span> <span class="re5">-s</span> <span class="re5">-o</span> mysql2.so client.o mysql2_ext.o result.o -L. -LC:<span class="sy0">/</span>Ruby192<span class="sy0">/</span>lib -L. -LC:\\Users\\Luis\\Projects\\oss\\oneclick\\rubyinstaller\\sandbox\\tk\\lib -LC:\\Users\\Luis\\Projects\\oss\\oneclick\\rubyinstaller\\sandbox\\tcl\\lib <span class="re5">-LC</span> :\\Users\\Luis\\Projects\\oss\\oneclick\\rubyinstaller\\sandbox\\libyaml\\lib <span class="re5">-L</span>
C:\\Users\\Luis\\Projects\\oss\\oneclick\\rubyinstaller\\sandbox\\pdcurses\\lib -LC:\\Users\\Luis\\Projects\\oss\\oneclick\\rubyinstaller\\sandbox\\openssl\\lib  -LC:\\Users\\Luis\\Projects\\oss\\oneclick\\rubyinstaller\\sandbox\\zlib\\lib -LC:\\Users\\Luis\\Projects\\oss\\oneclick\\rubyinstaller\\sandbox\\iconv\\lib <span class="re5">-L</span>
C:\\Users\\Luis\\Projects\\oss\\oneclick\\rubyinstaller\\sandbox\\gdbm\\lib -LC:\\Users\\Luis\\Projects\\oss\\oneclick\\rubyinstaller\\sandbox\\libffi\\lib  -Wl,--enable-auto-image-base,--enable-auto-import mysql2-i386-mingw32.def  <span class="re5">-lmsvcrt</span> <span class="re5">-ruby191</span> <span class="re5">-llibmysql</span>  <span class="re5">-lshell32</span> -lws2_32
client.o: In <span class="kw1">function</span> <span class="sy0">`</span>nogvl_connect<span class="st_h">':
C:\Ruby192\lib\ruby\gems\1.9.1\gems\mysql2-0.3.6\ext\mysql2/client.c:114: undefined reference to `mysql_real_connect@32'</span>
client.o: In <span class="kw1">function</span> <span class="sy0">`</span>nogvl_init<span class="st_h">':
C:\Ruby192\lib\ruby\gems\1.9.1\gems\mysql2-0.3.6\ext\mysql2/client.c:105: undefined reference to `mysql_init@4'</span>
client.o: In <span class="kw1">function</span> <span class="sy0">`</span>set_ssl_options<span class="st_h">':
C:\Ruby192\lib\ruby\gems\1.9.1\gems\mysql2-0.3.6\ext\mysql2/client.c:700: undefined reference to `mysql_ssl_set@24'</span>
collect2: <span class="kw2">ld</span> returned <span class="nu0">1</span> <span class="kw3">exit</span> status
make: <span class="sy0">***</span> <span class="br0">&#91;</span>mysql2.so<span class="br0">&#93;</span> Error <span class="nu0">1</span>
Gem files will remain installed <span class="kw1">in</span> C:<span class="sy0">/</span>Ruby192<span class="sy0">/</span>lib<span class="sy0">/</span>ruby<span class="sy0">/</span>gems<span class="sy0">/</span>1.9.1<span class="sy0">/</span>gems<span class="sy0">/</span>mysql2-0.3.6 <span class="kw1">for</span> inspection.
Results logged to C:<span class="sy0">/</span>Ruby192<span class="sy0">/</span>lib<span class="sy0">/</span>ruby<span class="sy0">/</span>gems<span class="sy0">/</span>1.9.1<span class="sy0">/</span>gems<span class="sy0">/</span>mysql2-0.3.6<span class="sy0">/</span>ext<span class="sy0">/</span>mysql2<span class="sy0">/</span>gem_make.out</pre></div></div></div></div></div></div></div>


<p>In other words, yet get a bunch of undefined references to functions that <em>should</em> exist in <em>libmysql.dll</em>. Except they don&#8217;t, because you&#8217;re running MySQL 64-bits and its functions have a different signature.</p>
<p>The solution is this: get the installation files for MySQL 32-bits (same version as yours), and copy the <em>libmysql.dll</em> file to your Ruby installation&#8217;s library directory&nbsp; (<em>C:\Ruby192\lib, for instance</em>). Now linking will succeed.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.websofia.com/2011/08/installing-rubys-mysql2-gem-for-mysql-64-bits/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Automating website &amp; MySQL backups</title>
		<link>http://www.websofia.com/2011/08/automating-website-mysql-backups/</link>
		<comments>http://www.websofia.com/2011/08/automating-website-mysql-backups/#comments</comments>
		<pubDate>Wed, 10 Aug 2011 09:26:56 +0000</pubDate>
		<dc:creator>alex</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Web development]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[cron]]></category>
		<category><![CDATA[crontab]]></category>
		<category><![CDATA[mysqldump]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.websofia.com/?p=137</guid>
		<description><![CDATA[I have a web server with a number of clients&#8217; websites on it. It&#8217;s necessary to backup these websites every day, since clients use a content management system to make changes regularly. These changes can be updates to a website&#8217;s &#8230;<p class="read-more"><a href="http://www.websofia.com/2011/08/automating-website-mysql-backups/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>I have a web server with a number of clients&#8217; websites on it. It&#8217;s necessary to backup these websites every day, since clients use a content management system to make changes regularly. These changes can be updates to a website&#8217;s MySQL database, or they can be changes to the files stored within these websites. What I&#8217;d like is to backup the MySQL database and the filesystem for each website, every day, at a specific time. The backups must rotate: when there are, say, five backups, I want the oldest one to be removed as the newest one is written. Also, I&#8217;d like the backup solution to send me an email every day after it&#8217;s completed the backups with a summary of the procedure.</p>
<p>So, in summary, my needs are these:</p>
<ul>
<li>Define a list of websites to back up</li>
<li>For each site, backup (dump) the MySQL database</li>
<li>For each site, backup the website&#8217;s file structure</li>
<li>Send an email to one or more people with a summary of the backup process.</li>
</ul>
<p>It&#8217;s possible to do this with a shell script (like <a href="http://sourceforge.net/projects/automysqlbackup/">AutoMySQLBackup</a> does). However, AutoMySQLBackup does not backup file systems or send email. Also, shell scripting tends to be messy code, so I decided to use Ruby.</p>
<h3>Configuration file</h3>
<p>First off, I&#8217;d like to store the list of websites to backup in a separate configuration file so that I can edit this list easily. Also, for reusability, I&#8217;ll store database access credentials and email addresses there too. The simplest way of making a configuration file to be read by Ruby is to actually write the configuration file <em>in</em> Ruby, like so:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="ruby"><pre class="de1">BACKUPDIR = <span class="st0">&quot;/backup/webserver&quot;</span>
ROTATE = <span class="nu0">5</span>
DBUSER = <span class="st0">&quot;root&quot;</span>
DBPASSWORD = <span class="st0">&quot;myrootpassword&quot;</span>
EMAILS = <span class="br0">&#91;</span> <span class="st0">&quot;alex@email.com&quot;</span>, <span class="st0">&quot;john@email.com&quot;</span> <span class="br0">&#93;</span>
WEBSITES = <span class="br0">&#123;</span>
 <span class="st0">&quot;sample.com&quot;</span> <span class="sy0">=&gt;</span> <span class="br0">&#123;</span>
   <span class="st0">&quot;path&quot;</span> <span class="sy0">=&gt;</span> <span class="st0">&quot;/usr/local/www/apache22/data/sample.com&quot;</span>,
   <span class="st0">&quot;database&quot;</span> <span class="sy0">=&gt;</span> <span class="st0">&quot;sampledb&quot;</span>
 <span class="br0">&#125;</span>,
 <span class="st0">&quot;example.net&quot;</span> <span class="sy0">=&gt;</span> <span class="br0">&#123;</span>
   <span class="st0">&quot;path&quot;</span> <span class="sy0">=&gt;</span> <span class="st0">&quot;/usr/local/www/apache22/data/example.net&quot;</span>,
   <span class="st0">&quot;database&quot;</span> <span class="sy0">=&gt;</span> <span class="st0">&quot;exampledb&quot;</span>
 <span class="br0">&#125;</span>
<span class="br0">&#125;</span></pre></div></div></div></div></div></div></div>


<p>This file stores a variable ROTATE which indicates the number of backups to keep before throwing away the oldest one. For each website, I specify the path to the files to be backed up, and the name of the MySQL database. The configuration file will be included and parsed automatically by the backup script, since it is plain Ruby code.</p>
<h3>Backup script</h3>
<p>The backup script begins by requiring SMTP support, so that we can send emails later. It also starts an output buffer (&#8220;output&#8221;) where we will store all messages generated by the script to be included in the email. Before starting the backup procedure, we start a begin&#8230;rescue block so that me may catch any exceptions thrown by Ruby, in order to include these in the email as well.</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="ruby"><pre class="de1"><span class="kw3">require</span> <span class="st0">&quot;net/smtp&quot;</span>
output = <span class="st0">&quot;Webserver backup script&quot;</span>
<span class="kw1">begin</span>
  <span class="co1"># Load config file:</span>
  <span class="kw3">require</span> <span class="st0">&quot;/usr/home/alex/backup-script/config.rb&quot;</span>
  <span class="co1"># Does the backup directory exist?</span>
  <span class="kw1">if</span> <span class="kw1">not</span> <span class="kw4">FileTest</span>::exists?<span class="br0">&#40;</span>BACKUPDIR<span class="br0">&#41;</span>
    <span class="kw3">raise</span> <span class="st0">&quot;Backup directory #{BACKUPDIR} does not exist.&quot;</span>
    <span class="kw3">exit</span>
  <span class="kw1">end</span></pre></div></div></div></div></div></div></div>


<p>The script now loops through the list of websites defined in the configuration file, creating a backup directory with the name of the website for each if it doesn&#8217;t already exist:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="ruby"><pre class="de1">  WEBSITES.<span class="me1">each</span> <span class="kw1">do</span> <span class="sy0">|</span>name, website<span class="sy0">|</span>
&nbsp;
    output <span class="sy0">&lt;&lt;</span> <span class="st0">&quot;<span class="es0">\r</span><span class="es0">\n</span><span class="es0">\r</span><span class="es0">\n</span>Backing up #{name}:&quot;</span>
&nbsp;
    <span class="co1"># Establish backup dir</span>
    path = BACKUPDIR <span class="sy0">+</span> <span class="st0">&quot;/&quot;</span> <span class="sy0">+</span> name
&nbsp;
    <span class="co1"># If website dir does not exist, create it.</span>
    <span class="kw1">if</span> <span class="kw1">not</span> <span class="kw4">FileTest</span>::exists?<span class="br0">&#40;</span>path<span class="br0">&#41;</span>
      <span class="kw4">Dir</span>.<span class="me1">mkdir</span> path
      output <span class="sy0">&lt;&lt;</span> <span class="st0">&quot;<span class="es0">\r</span><span class="es0">\n</span>  Directory #{path} created.&quot;</span>
    <span class="kw1">end</span></pre></div></div></div></div></div></div></div>


<p>Next, the script enumerates the subdirectories that already exist in the website&#8217;s backup directory. This is because we will create a subdirectory with date backup&#8217;s date for each backup (e.g. 20110810-105535, for 10 August 2011, 10:55:35). These directories are then sorted alphabetically, so that the least recent backup of the website is first in the list.</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="ruby"><pre class="de1">    <span class="co1"># Get entries inside dir with modification times (sorted first to last)</span>
    entries = <span class="br0">&#91;</span><span class="br0">&#93;</span>
    <span class="kw4">Dir</span>.<span class="me1">entries</span><span class="br0">&#40;</span>path<span class="br0">&#41;</span>.<span class="me1">each</span> <span class="kw1">do</span> <span class="sy0">|</span>entry<span class="sy0">|</span>
      <span class="kw1">next</span> <span class="kw1">if</span> entry == <span class="st0">&quot;.&quot;</span> <span class="kw1">or</span> entry == <span class="st0">&quot;..&quot;</span>
      mtime = <span class="kw4">File</span>.<span class="me1">mtime</span><span class="br0">&#40;</span>path <span class="sy0">+</span> <span class="st0">&quot;/&quot;</span> <span class="sy0">+</span> entry<span class="br0">&#41;</span>.<span class="me1">to_f</span>
      entries <span class="sy0">&lt;&lt;</span> <span class="br0">&#91;</span> mtime, entry <span class="br0">&#93;</span>
    <span class="kw1">end</span>
    entries.<span class="me1">sort</span>! <span class="br0">&#123;</span> <span class="sy0">|</span>x,y<span class="sy0">|</span> x<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> <span class="sy0">&lt;=&gt;</span> y<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> <span class="br0">&#125;</span>
    output <span class="sy0">&lt;&lt;</span> <span class="st0">&quot;<span class="es0">\r</span><span class="es0">\n</span>  #{entries.length} backups found (max #{ROTATE-1}).&quot;</span></pre></div></div></div></div></div></div></div>


<p>The total number of backups found is compared to the value of ROTATE. If there are too many backups, the latest one(s) (first in the list) are removed.</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="ruby"><pre class="de1">    <span class="co1"># Remove least recent entries if more than ROTATE available:</span>
    <span class="kw1">while</span> entries.<span class="me1">length</span> <span class="sy0">&gt;</span> ROTATE <span class="sy0">-</span> <span class="nu0">1</span>
      entry = entries.<span class="me1">shift</span>
      cmd = <span class="st0">&quot;rm -R -f #{path}/#{entry[1]}&quot;</span>
      <span class="st0">`#{cmd}`</span>
      output <span class="sy0">&lt;&lt;</span> <span class="st0">&quot;<span class="es0">\r</span><span class="es0">\n</span>  Removed #{path}/#{entry[1]}&quot;</span>
    <span class="kw1">end</span></pre></div></div></div></div></div></div></div>


<p>Having cleaned up excess backups, the script now creates a fresh folder, naming it with the current date and time:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="ruby"><pre class="de1">    <span class="co1"># Create new folder for backup:</span>
    subdir = <span class="kw4">Time</span>.<span class="me1">now</span>.<span class="me1">strftime</span><span class="br0">&#40;</span><span class="st0">&quot;%Y%m%d-%H%M%S&quot;</span><span class="br0">&#41;</span>
    <span class="kw4">Dir</span>.<span class="me1">mkdir</span> path <span class="sy0">+</span> <span class="st0">&quot;/&quot;</span> <span class="sy0">+</span> subdir
    output <span class="sy0">&lt;&lt;</span> <span class="st0">&quot;<span class="es0">\r</span><span class="es0">\n</span>  Created directory #{path}/#{subdir}&quot;</span></pre></div></div></div></div></div></div></div>


<p>If a website has a database defined in the configuration file, the script now calls <strong>mysqldump</strong> to create a backup of the database inside the newly created backup subdirectory. The backup is gzipped as well. <em>Note that a full path to mysqldump must be provided, since <strong>cron</strong>, which we will use later to run our script at specific times, does not include a path to mysqldump in the shell that it runs in.</em></p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="ruby"><pre class="de1">    <span class="co1"># Dump database (if required)</span>
    <span class="kw1">if</span> website.<span class="me1">has_key</span>? <span class="st0">&quot;database&quot;</span>
      <span class="co1"># Dump db</span>
      cmd = <span class="st0">&quot;/usr/local/bin/mysqldump -u#{DBUSER} -p#{DBPASSWORD} #{website[&quot;</span>database<span class="st0">&quot;]} | gzip &gt; #{path}/#{subdir}/#{name}.sql.gz&quot;</span>
      <span class="st0">`#{cmd}`</span>
      output <span class="sy0">&lt;&lt;</span> <span class="st0">&quot;<span class="es0">\r</span><span class="es0">\n</span>  Dumped database #{website[&quot;</span>database<span class="st0">&quot;]} to #{path}/#{subdir}/#{name}.sql.gz&quot;</span>
    <span class="kw1">end</span></pre></div></div></div></div></div></div></div>


<p>If a website has a path to files defined in the configuration file, the script now uses <strong>tar/gzip</strong> to create a tarball of the entire website file structure, recursing into subdirectories.</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="ruby"><pre class="de1">    <span class="co1"># Dump code (if required)</span>
    <span class="kw1">if</span> website.<span class="me1">has_key</span>? <span class="st0">&quot;path&quot;</span>
      <span class="co1"># Copy code</span>
      <span class="st0">`cd #{website[&quot;path&quot;]}; tar -czf #{path}/#{subdir}/#{name}.tar.gz *`</span>
      output <span class="sy0">&lt;&lt;</span> <span class="st0">&quot;<span class="es0">\r</span><span class="es0">\n</span>  Created zipped tarball of code in #{path}/#{subdir}/code&quot;</span>
    <span class="kw1">end</span>
  <span class="kw1">end</span></pre></div></div></div></div></div></div></div>


<p>This completes the loop that backs up all the websites. We now end our <strong>rescue</strong> clause in order to catch any exception thrown by Ruby during this process. The exception text is appended to the running log (<em>output</em>) as well as written to standard output.</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="ruby"><pre class="de1"><span class="kw1">rescue</span> <span class="kw4">StandardError</span> <span class="sy0">=&gt;</span> error
  output <span class="sy0">&lt;&lt;</span> <span class="st0">&quot;Error occurred: &quot;</span> <span class="sy0">+</span> error
  <span class="kw3">puts</span> <span class="st0">&quot;Error occurred: &quot;</span> <span class="sy0">+</span> error
<span class="kw1">end</span></pre></div></div></div></div></div></div></div>


<p>All that is left to do is to send the output off through email. This is easy to do (any one reason we&#8217;re using Ruby):</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="ruby"><pre class="de1"><span class="co1"># Mail output:</span>
<span class="re2">Net::SMTP</span>.<span class="me1">start</span><span class="br0">&#40;</span><span class="st0">'127.0.0.1'</span><span class="br0">&#41;</span> <span class="kw1">do</span> <span class="sy0">|</span>smtp<span class="sy0">|</span>
  output = <span class="st0">&quot;Subject: Webserver backup procedure<span class="es0">\r</span><span class="es0">\n</span>&quot;</span> <span class="sy0">+</span> output
  EMAILS.<span class="me1">each</span> <span class="kw1">do</span> <span class="sy0">|</span>email<span class="sy0">|</span>
    smtp.<span class="me1">send_message</span> output, <span class="st0">&quot;alex@email.com&quot;</span>, email
  <span class="kw1">end</span>
<span class="kw1">end</span></pre></div></div></div></div></div></div></div>


<h3>Adding the script to cron</h3>
<p>We can now add the script to the system&#8217;s <strong>crontab</strong> in order to run at regular times. We&#8217;ll write a small shell script that launches the script using the bash shell, to make sure that cron has access to a powerful shell to run in:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="bash"><pre class="de1"><span class="co0">#/usr/local/bin/bash</span>
<span class="sy0">/</span>usr<span class="sy0">/</span>local<span class="sy0">/</span>bin<span class="sy0">/</span>ruby <span class="sy0">/</span>usr<span class="sy0">/</span>home<span class="sy0">/</span>alex<span class="sy0">/</span>backup-script<span class="sy0">/</span>backup.rb</pre></div></div></div></div></div></div></div>


<p>The following entry is added to the system crontab (<em>/etc/crontab</em>). This will make sure that the script runs every day at 22:00.</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="bash"><pre class="de1"><span class="co0"># Run webserver backup script</span>
00      <span class="nu0">22</span>      <span class="sy0">*</span>       <span class="sy0">*</span>       <span class="sy0">*</span>       root    <span class="sy0">/</span>usr<span class="sy0">/</span>home<span class="sy0">/</span>alex<span class="sy0">/</span>backup-script<span class="sy0">/</span>backup.sh</pre></div></div></div></div></div></div></div>


]]></content:encoded>
			<wfw:commentRss>http://www.websofia.com/2011/08/automating-website-mysql-backups/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SASS and CSScaffold</title>
		<link>http://www.websofia.com/2011/03/sass-and-csscaffold/</link>
		<comments>http://www.websofia.com/2011/03/sass-and-csscaffold/#comments</comments>
		<pubDate>Thu, 31 Mar 2011 19:08:38 +0000</pubDate>
		<dc:creator>alex</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web development]]></category>
		<category><![CDATA[PHP SASS CSS CSScaffold Smarty mixin]]></category>

		<guid isPermaLink="false">http://www.websofia.com/?p=133</guid>
		<description><![CDATA[I think the concept that SASS brings to the table (or CSScaffold, for that matter) is one we&#8217;ve all had when we play with CSS and think, &#8220;Gee, I would be nice if you could use variables and constants here, &#8230;<p class="read-more"><a href="http://www.websofia.com/2011/03/sass-and-csscaffold/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>I think the concept that <a href="http://sass-lang.com">SASS</a> brings to the table (or <a href="https://github.com/sunny/csscaffold">CSScaffold</a>, for that matter) is one we&#8217;ve all had when we play with CSS and think, &#8220;Gee, I would be nice if you could use variables and constants here, and if you could duplicate less code.&#8221; And then we would think of splitting our CSS up into many little files, since they&#8217;re easier to organize by function, only to find that that wasn&#8217;t such a hot idea because a browser will have to make a new HTTP connection for each one to download it.</p>
<p>So here&#8217;s SASS/CSScaffold adding just those features that CSS was missing. But is it all good news? I&#8217;d say on the whole, yes, but here&#8217;s a few points:</p>
<p>SASS requires that you compile your stylesheets every time you update them. My typical development cycle is make a little change to CSS (one one monitor) and hit refresh in my browser (other monitor) to see if the change did what I wanted it to do. That would have to change: now I would need to compile my CSS before I hit refresh. Not insurmountable, but it&#8217;s one more thing I can and will forgot and then I&#8217;ll think, &#8220;Hey, now why didn&#8217;t that change do anything?&#8221; only to find out after some head-scratching that I forgot to compile.</p>
<p>CSScaffold doesn&#8217;t seem to have this problem: since it&#8217;s written in PHP, it&#8217;ll run every time the CSS is requested from the server. I&#8217;m sure the authors have built in some sort of caching, so it should be quick enough. That actually sounds handier to me than SASS does, merely because I don&#8217;t need to compile. So the question is then, is CSScaffold just as good, better, or worse? If it&#8217;s just as good, I&#8217;ll go with it instead of SASS!</p>
<p>But is what SASS/CSScaffold do really that new? Like I said at the start of this post, it&#8217;s an idea all of us have thought of&#8230; and implemented! It&#8217;s always been possible to produce CSS through PHP. You can put a link to a PHP file in your page&#8217;s header, have it output a text/css header and you&#8217;re good to go. That&#8217;ll allow you to use variables, like SASS, constants, like SASS, functions and mixins, like SASS, all at zero cost since you had PHP anyway. You&#8217;ll basically only need to write the fancy gradient functions that SASS adds.</p>
<p>In order to add caching, you could pull your CSS through Smarty, thus prettifying the syntax a bit (it never feels quite right having PHP produce actual HTML or CSS what with the separation of code and presentation, so using Smarty gives a fuzzy warm feeling of righteousness). You could even write some spiffy new functions for Smarty, thus creating your own Sassy Smarty. So why all the hullabaloo?</p>
<p>Well, for one thing&#8230; SASS does more than I ever implemented with a CSS/PHP/Smarty approach, so hats off for that. But I still don&#8217;t like the compilation requirement.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.websofia.com/2011/03/sass-and-csscaffold/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dynamic CSS through PHP</title>
		<link>http://www.websofia.com/2011/03/dynamic-css-through-php/</link>
		<comments>http://www.websofia.com/2011/03/dynamic-css-through-php/#comments</comments>
		<pubDate>Thu, 31 Mar 2011 08:54:59 +0000</pubDate>
		<dc:creator>alex</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web development]]></category>
		<category><![CDATA[CSS PHP dynamic Smarty]]></category>

		<guid isPermaLink="false">http://www.websofia.com/?p=130</guid>
		<description><![CDATA[When writing CSS,you will find yourself repeating information a lot, which is always a bad thing in programming. CSS 2 lacks constants, which would allow us to define a value once and refer to it many times. Instead, we are &#8230;<p class="read-more"><a href="http://www.websofia.com/2011/03/dynamic-css-through-php/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>When writing CSS,you will find yourself repeating information a lot, which is always a bad thing in programming. CSS 2 lacks constants, which would allow us to define a value once and refer to it many times. Instead, we are forced to repeat the actual value many times, making updating CSS a process that is prone to errors.</p>
<p>Also, in order to reduce the number of connections a client must make to the server, it&#8217;s necessary to place all CSS in a single file. But this may mean that you end up with a lot of possibly unrelated CSS in a single file, making it difficult to navigate while you&#8217;re developing. There are times when it&#8217;s simply handier to have lots of  small files instead of one big file, but it&#8217;s just not practical for download by your visitors.</p>
<p>These two problems can be resolved by loading your CSS through PHP. Instead of serving the CSS file directly, i.e.</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="html"><pre class="de1">&lt;link rel=&quot;stylesheet&quot; href=&quot;css/style.css&quot; type=&quot;text/css&quot; media=&quot;screen&quot;/&gt;</pre></div></div></div></div></div></div></div>


<p>you can have the server load a PHP script that produces CSS like so:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="html"><pre class="de1">&lt;link rel=&quot;stylesheet&quot; href=&quot;css/style.css.php&quot; type=&quot;text/css&quot; media=&quot;screen&quot;/&gt;</pre></div></div></div></div></div></div></div>


<p>Note that this will only work if the scripts emits a text/css header:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="kw2">&lt;?php</span>
  <span class="kw3">header</span><span class="br0">&#40;</span><span class="st0">&quot;Content-type: text/css&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
  <span class="sy0">...</span>
<span class="sy1">?&gt;</span></pre></div></div></div></div></div></div></div>


<p>Now your PHP script can define some constants that you simply insert into your CSS:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="kw2">&lt;?php</span>
   <span class="kw3">header</span><span class="br0">&#40;</span><span class="st0">&quot;Content-type: text/css&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
   <span class="re0">$mycolor</span> <span class="sy0">=</span> <span class="st0">&quot;#aaa&quot;</span><span class="sy0">;</span>
<span class="sy1">?&gt;</span>
&nbsp;
p {
  line-height: 1.1em;
  color: <span class="kw2">&lt;?php</span> <span class="kw1">echo</span> <span class="re0">$mycolor</span><span class="sy0">;</span> <span class="sy1">?&gt;</span>
}</pre></div></div></div></div></div></div></div>


<p>Your script could also load various CSS files for processing and output the result in one go, solving the second problem we found. But we can do better still. You can have your PHP script use <a href="http://www.smarty.net">Smarty</a> to produce the CSS, making the use of contants easier (and prettier):</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="kw2">&lt;?php</span> 
  <span class="kw3">header</span><span class="br0">&#40;</span><span class="st0">&quot;Content-type: text/css&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span> 
  <span class="kw1">require_once</span> <span class="st0">&quot;../smarty/Smarty.class.php&quot;</span><span class="sy0">;</span>
  <span class="re0">$smarty</span> <span class="sy0">=</span> <span class="kw2">new</span> Smarty<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
  <span class="re0">$smarty</span><span class="sy0">-&gt;</span><span class="me1">template_dir</span> <span class="sy0">=</span> <span class="st0">&quot;../smarty/templates&quot;</span><span class="sy0">;</span>
  <span class="re0">$smarty</span><span class="sy0">-&gt;</span><span class="me1">compile_dir</span> <span class="sy0">=</span> <span class="st0">&quot;../smarty/templates_c&quot;</span><span class="sy0">;</span>
  <span class="re0">$smarty</span><span class="sy0">-&gt;</span><span class="me1">cache_dir</span> <span class="sy0">=</span> <span class="st0">&quot;../smarty/cache&quot;</span><span class="sy0">;</span>
  <span class="re0">$smarty</span><span class="sy0">-&gt;</span><span class="me1">config_dir</span> <span class="sy0">=</span> <span class="st0">&quot;../smarty/configs&quot;</span><span class="sy0">;</span>
  <span class="re0">$smarty</span><span class="sy0">-&gt;</span><span class="me1">compile_check</span> <span class="sy0">=</span> <span class="kw4">true</span><span class="sy0">;</span>
  <span class="re0">$smarty</span><span class="sy0">-&gt;</span><span class="me1">caching</span> <span class="sy0">=</span> <span class="nu0">0</span><span class="sy0">;</span>
  <span class="re0">$smarty</span><span class="sy0">-&gt;</span><span class="me1">display</span><span class="br0">&#40;</span><span class="st0">&quot;file:style.css&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="sy1">?&gt;</span></pre></div></div></div></div></div></div></div>


<p>The file style.css would be the main style sheet manifold; it could load other (sub-) stylesheets. For instance:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="br0">&#123;</span>assign <span class="kw2">var</span><span class="sy0">=</span><span class="st0">&quot;defaultfont&quot;</span> value<span class="sy0">=</span><span class="st0">&quot;normal 11px/1.2em Arial, sans-serif&quot;</span><span class="br0">&#125;</span>
<span class="br0">&#123;</span>assign <span class="kw2">var</span><span class="sy0">=</span><span class="st0">&quot;thinborder&quot;</span> value<span class="sy0">=</span><span class="st0">&quot;solid 1px #aaa&quot;</span><span class="br0">&#125;</span>
<span class="br0">&#123;</span>assign <span class="kw2">var</span><span class="sy0">=</span><span class="st0">&quot;inputcolor&quot;</span> value<span class="sy0">=</span><span class="st0">&quot;#666&quot;</span><span class="br0">&#125;</span>
<span class="br0">&#123;</span><span class="kw1">include</span> <span class="kw3">file</span><span class="sy0">=</span><span class="st0">&quot;sys/global-reset.css&quot;</span><span class="br0">&#125;</span>
<span class="br0">&#123;</span><span class="kw1">include</span> <span class="kw3">file</span><span class="sy0">=</span><span class="st0">&quot;sys/base.css&quot;</span><span class="br0">&#125;</span>
<span class="br0">&#123;</span><span class="kw1">include</span> <span class="kw3">file</span><span class="sy0">=</span><span class="st0">&quot;sys/loader.css&quot;</span><span class="br0">&#125;</span>
<span class="br0">&#123;</span><span class="kw1">include</span> <span class="kw3">file</span><span class="sy0">=</span><span class="st0">&quot;control/accordion.css&quot;</span><span class="br0">&#125;</span>
<span class="br0">&#123;</span><span class="kw1">include</span> <span class="kw3">file</span><span class="sy0">=</span><span class="st0">&quot;control/ajaxtable.css&quot;</span><span class="br0">&#125;</span>
<span class="br0">&#123;</span><span class="kw1">include</span> <span class="kw3">file</span><span class="sy0">=</span><span class="st0">&quot;control/button.css&quot;</span><span class="br0">&#125;</span></pre></div></div></div></div></div></div></div>


<p>The values that were assigned to defaultfont, thinborder and inputcolor can be used in the sub-stylesheets like so:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1">input
<span class="br0">&#123;</span>
  border<span class="sy0">:</span> <span class="br0">&#123;</span><span class="re0">$thinborder</span><span class="br0">&#125;</span><span class="sy0">;</span>
  color<span class="sy0">:</span> <span class="br0">&#123;</span><span class="re0">$inputcolor</span><span class="br0">&#125;</span><span class="sy0">;</span>
<span class="br0">&#125;</span></pre></div></div></div></div></div></div></div>


]]></content:encoded>
			<wfw:commentRss>http://www.websofia.com/2011/03/dynamic-css-through-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>FireFox 4 does not like script.aculo.us builder</title>
		<link>http://www.websofia.com/2011/03/firefox-4-does-not-like-script-aculo-us-builder/</link>
		<comments>http://www.websofia.com/2011/03/firefox-4-does-not-like-script-aculo-us-builder/#comments</comments>
		<pubDate>Wed, 30 Mar 2011 09:45:27 +0000</pubDate>
		<dc:creator>alex</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Web development]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[builder]]></category>
		<category><![CDATA[FF4]]></category>
		<category><![CDATA[FireFox 4]]></category>
		<category><![CDATA[head]]></category>
		<category><![CDATA[prototype]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[script.aculo.us]]></category>

		<guid isPermaLink="false">http://www.websofia.com/?p=123</guid>
		<description><![CDATA[After upgrading to FF4 I noticed that some of my JavaScript, which had been working perfectly fine, stopped working. I was able to isolate the problem to the use of the script.aculo.us Builder class to create a script element, like &#8230;<p class="read-more"><a href="http://www.websofia.com/2011/03/firefox-4-does-not-like-script-aculo-us-builder/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>After upgrading to FF4 I noticed that some of my JavaScript, which had been working perfectly fine, stopped working. I was able to isolate the problem to the use of the script.aculo.us Builder class to create a <em>script</em> element, like so:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="javascript"><pre class="de1"><span class="kw2">var</span> head <span class="sy0">=</span> $$<span class="br0">&#40;</span><span class="st0">&quot;head&quot;</span><span class="br0">&#41;</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span><span class="sy0">;</span>
js <span class="sy0">=</span> Builder.<span class="me1">node</span><span class="br0">&#40;</span><span class="st0">&quot;script&quot;</span><span class="sy0">,</span> <span class="br0">&#123;</span> type<span class="sy0">:</span> <span class="st0">&quot;text/javascript&quot;</span><span class="sy0">,</span> src<span class="sy0">:</span> path <span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span>
js.<span class="me1">onreadystatechange</span> <span class="sy0">=</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="kw1">if</span> <span class="br0">&#40;</span>js.<span class="me1">readyState</span> <span class="sy0">==</span> <span class="st0">'loaded'</span>
  <span class="sy0">||</span> js.<span class="me1">readyState</span> <span class="sy0">==</span> <span class="st0">'complete'</span><span class="br0">&#41;</span> js.<span class="kw3">onload</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="br0">&#125;</span><span class="sy0">;</span>
js.<span class="kw3">onload</span> <span class="sy0">=</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> console.<span class="me1">log</span><span class="br0">&#40;</span><span class="st0">&quot;loaded!&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="br0">&#125;</span><span class="sy0">;</span>
head.<span class="me1">insert</span><span class="br0">&#40;</span>js<span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></div></div></div></div></div>


<p>However, the <em>onload</em> event would never be triggered. In fact, Firebug indicates that the JavaScript file I&#8217;m trying to load is never actually loaded from the server. So it&#8217;s back to basics without using script.aculo.us&#8217;s Builder:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="javascript"><pre class="de1"><span class="kw2">var</span> head <span class="sy0">=</span> $$<span class="br0">&#40;</span><span class="st0">&quot;head&quot;</span><span class="br0">&#41;</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span><span class="sy0">;</span>
<span class="kw2">var</span> js <span class="sy0">=</span> document.<span class="me1">createElement</span><span class="br0">&#40;</span><span class="st0">'script'</span><span class="br0">&#41;</span><span class="sy0">;</span>
js.<span class="me1">type</span> <span class="sy0">=</span> <span class="st0">'text/javascript'</span><span class="sy0">;</span>
js.<span class="me1">onreadystatechange</span> <span class="sy0">=</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="kw1">if</span> <span class="br0">&#40;</span>js.<span class="me1">readyState</span> <span class="sy0">==</span> <span class="st0">'loaded'</span>
  <span class="sy0">||</span> js.<span class="me1">readyState</span> <span class="sy0">==</span> <span class="st0">'complete'</span><span class="br0">&#41;</span> js.<span class="kw3">onload</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="br0">&#125;</span><span class="sy0">;</span>
js.<span class="kw3">onload</span> <span class="sy0">=</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> console.<span class="me1">log</span><span class="br0">&#40;</span><span class="st0">&quot;loaded&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="br0">&#125;</span><span class="sy0">;</span>
js.<span class="me1">src</span> <span class="sy0">=</span> path<span class="sy0">;</span>
head.<span class="me1">appendChild</span><span class="br0">&#40;</span>js<span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></div></div></div></div></div>


<p>And guess what: this works. The file is loaded. Now why does this happen? The new <em>script </em>element is in fact added to the DOM; I can see that in Firebug. But it never loads the JavaScript from the server.</p>
<p>Playing around with script.aculo.us&#8217;s <em>builder.js</em> shows that the <em>script </em>tag cannot be created through <em>innerHTML</em> but must be created through <em>document.createElement</em> instead. I don&#8217;t know why script.aculo.us tries the <em>innerHTML</em> approach first, but it does &#8211; and it works. It just doesn&#8217;t load the javascript file. If I deliberately make the <em>innerHTML </em>approach fail, it falls back to <em>document.createElement</em>, which works.</p>
<p>This is not the whole story, though. When adding attributes to the newly created element, <em>builder.js</em> again tries to use <em>innerHTML</em> before using <em>document.create</em>. And again, skipping <em>innerHTML</em> to make it fall back to <em>document.create</em> works.</p>
<p>The reason <em>innerHTML </em>is used can be found <a href="http://dev.rubyonrails.org/ticket/2707">here</a>, according to the source, but I could not access this URL at the time of this writing.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.websofia.com/2011/03/firefox-4-does-not-like-script-aculo-us-builder/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

