I'm writing this blog entry offline.
When I first moved in, my professor warned me that he didn't think the signal from his wireless router reached the far bedroom in which I'm staying. “That's ok,” I said, “I'm getting 2 bars; not great but it's a usable signal.”
After I'd been using the network for a few days, I happened to mention to him that I'm getting horribly slow speeds on his “linksys” wireless network. “I don't have a Linksys router,” he said. “What?” I asked. “I don't have a Linksys router. I have a D-Link router.” “But... then how... who...” “You must be getting a neighbor's signal.” “Ah. I see.”
Since my connection to a neighbor's internet was both unreliable and slow, I would much rather be on my professor's cable modem connection. I was pretty sure the lack of a signal from his router was a configuration issue rather than a signal limitation, so I went to go check the configuration. At this time it was well after midnight, and not wanting to disturb my professor's sleep, I resolved to hack into the router rather than wake him up and ask him for the password.
In the pitch-black living room, I used the light of my laptop screen to illuminate the mess of cables around his router and cable modem. I'd have to have a copper hard-link until I got the wireless working. I found a spare ethernet port on the router and plugged myself in. Turn on DHCP... then run ipconfig, good: it gave me an ip address. Now that I had a link, I needed to access the router's configuration page. Nine times out of ten, the ip address of a router is 192.168.1.1. I opened up my Firefox web browser and typed in that address. It popped up and asked me for a username and password. So far so good – I wasn't in yet, but at least I knew there is a router at that address.
I never get discouraged when I see a dialog for a password and I don't know it, because I've learned a few very simple facts: 1. most users never change the default passwords, 2. default passwords are remarkably easy to guess. Besides, I had a gut feeling the login would be “admin/admin.”
That brought up the router configuration page. Now, to check the “wireless” section. I clicked on it, but instead of any configuration options, I was treated to this helpful message: ~!@ #$%^ &*()_
The software that is loaded on home network appliances has never been particularly well-written, but lately it has been getting even worse. In this case, the router's configuration software is so badly broken, that it returns a page containing random binary garbage, instead of the wireless configuration page.
Upgrading the firmware on the router to fix this problem would be more work and more intrusive than my original intentions, so I prepared to give up for the night. Then I noticed that the color scheme of the configuration page was purple and grey, rather than the blue and grey D-Link would use. Guess which router company favors puple over blue? Yep, Linksys. I missed the Linksys logo when I first logged in because their broken software just loads an empty image box where their logo goes. But a quick check of the ip addresses on my interfaces showed my error: I should have been on 192.168.0.x if I wanted to connect over my copper ethernet. What I used, 192.168.1.x, was my wireless interface. Darn it, I broke into the wrong router. Sorry neighbor dude! How embarassing – like walking into the Women's bathroom by mistake.
With the correct IP address in my browser, I got to another login screen, this time for the D-Link router. “Admin/admin” didn't work here. Hmmm... maybe, “administrator/password”? Nope. I tried a few more obvious combinations and failed. Time to back up and think about this analytically. Now I know that most login systems don't allow blank passwords, but some do. So perhaps I had been overthinking it. Had I failed to guess the password simply because there wasn't any? Indeed. “Admin/[blank]” worked like a charm.
Once I got into the right router, the only thing I could see that might be wrong was that WEP encryption was enabled. Considering that the router login was admin with a blank password, I highly doubted my professor had been in these configuration pages at all, so I don't think he deliberately enabled WEP. While I would be taking off a layer of security by turning off WEP encryption, if neither he nor I could access his wireless network at all, than the WEP security was clearly not doing any good. As bad as it is to have your wireless network unsecured, WEP has caused me nothing but trouble. Every time I try to use it to secure a network, I've had to turn it back off again because half of the computers I want to connect, can't.
With WEP turned off, the wireless network began working again. Still no internet. Why not?
At this point it's best to approach the problem in stages. Can I ping my own address? Good. Can I ping the router? Good. Can I ping http://www.google.com/? No. What's between the router and google? Ah, yes, the cable modem. Cable modems are notorious for crashing and needing to be rebooted, so naturally they are designed without an accessible reset button and with no on/off switch to power cycle them. How hard would it have been to put a watch-dog timer in the hardware that reboots the modem if it can't ping out? These devices are designed with the assumption that nothing will go wrong. If these companies would admit to themselves that software is inherently unreliable, it would be the first step to making devices that are reliable.
I unplugged the router and waited five seconds before plugging it in. Instead of connecting me to the internet, the router flashed its power and link lights over and over. What the heck does that mean? Turning it off and on again did not stop the flashing. Hmm... If the router has a configuration page, surely the cable modem has one too. How to access it? In the router configuration page, the local network is 192.168.0.x; but the outside network is 192.168.100.x. If that is the network the router is using to talk to the cable modem, that would put the cable modem at 192.168.100.1.
The cable modem didn't require a password, because it doesn't have any user-changeable options. It does show status though. It said “LAN: Up” “USB: Down” (no USB is plugged in) and “CABLE: Down”. Bummer. The modem's event log page had more bad news: “DHCP failed” “Timing Sync Failure” “Failed to receive....” “Failed to receive...” (In other words, the cable modem's not getting any response from the cable company.)
Well, fiddlesticks. After all that, I still have no internet.
It turned out that the cable company was having technical problems; cable internet service was down in the entire area for nearly 24 hours.
Tuesday, May 8, 2007
Thursday, May 3, 2007
Giant Mackerel Gets Married
Lent is a time when Catholics try to identify something in their lives that is a bad habit, and sacrifice it for a month. I gave up Reddit.com for Lent. Actually, what I gave up was wasting time on the internet. I'm not sure there's a distinction.
It didn't go so well. I think I ended up reading Reddit more when I was trying not to, than I ever did before or since the Lenten period. At one point I reached Reddit link number #999. That's like sitting down to eat a few Cheetos, and ending up eating a whole bag instead of dinner.
It seems that all bad habits share a common feature - they involve consequences such that one might decide a priori that the activity is not worthwhile, but when broken down into pieces, each infinitesimal step does not entail enough harm to cause someone to stop. One more potato chip, one more cigarette (thank God I don't smoke), reading one more Reddit article - I used to think this kind of procrastination was my own peculiar personality flaw. I'm beginning to realize I'm not as alone in this as I thought.
In a remarkable expression of candid self-awareness, one of the founders of Reddit blogs about this very problem. In that well-written post, Aaron Swartz characterizes the basic problem: people might rather do things more substantial with their time, but "reading bite-sized blog posts is by far easier," and "looking at photos of sunsets or reading one-liners takes no cognitive effort."
I took a Bioinformatics class last year that was co-taught by professors from computer science and biology. The biology professor wanted to underscore the importance of the discovery of DNA, or perhaps just had a chip on his shoulder; he took pleasure in baiting the minority of computer science students in the class by telling us that the invention of the computer is nothing, completely irrelevant, compared to the enormity of the discovery of DNA and genetic engineering, and that we should all stop studying computers and study biology instead. Rather than let him irritate me, I only smiled - because it is laughable that he would say this, while not realizing his whole syllabus relies on a third technology which eclipses both in significance: the web.
So what do we do with the greatest invention of all time? What are we using the internet for? If you have Ruby (a computer language) installed on your computer, I can save you the trouble of reading 999 reddit links to find out. This Ruby program (from my Downloads page) rips every current reddit title and runs a modified Markov learning algorithm on the titles. Then it uses what it learned to generate sample titles in reddit "style". What results is a hilarious mis-mash. I'll leave you with a few of the most interesting tidbits it generated (these are machine-generated; I didn't make these up):
Giant Mackerel Gets Married
Giant Mackerel Gets Even Creepier
Baby Marmosets Are To Analyze Software Patents
Cockatoo Guarding Chocolate Good, Morbid Obesity Soaring
Even worse than one peep jousting: Two peeps
Anarchy On Sudafed Claims First Free Gps
Extreme D.I.Y. - Solitaire
Doomsday For How Blogging Can Die Of Education
Reddit Users To Prevent Manatee Deaths? Prevent Manatees!
Ask Reddit: what modern world?
I Read Reddit Users Are Small. [pic] This Pathetic Story
Anatomy Of Yoda [photo]
sex-mad? Mabye. Obsessed? With Chainsaw Could possibly be stopped
Grindhouse: Good movie or faked orgasm?
It didn't go so well. I think I ended up reading Reddit more when I was trying not to, than I ever did before or since the Lenten period. At one point I reached Reddit link number #999. That's like sitting down to eat a few Cheetos, and ending up eating a whole bag instead of dinner.
It seems that all bad habits share a common feature - they involve consequences such that one might decide a priori that the activity is not worthwhile, but when broken down into pieces, each infinitesimal step does not entail enough harm to cause someone to stop. One more potato chip, one more cigarette (thank God I don't smoke), reading one more Reddit article - I used to think this kind of procrastination was my own peculiar personality flaw. I'm beginning to realize I'm not as alone in this as I thought.
In a remarkable expression of candid self-awareness, one of the founders of Reddit blogs about this very problem. In that well-written post, Aaron Swartz characterizes the basic problem: people might rather do things more substantial with their time, but "reading bite-sized blog posts is by far easier," and "looking at photos of sunsets or reading one-liners takes no cognitive effort."
I took a Bioinformatics class last year that was co-taught by professors from computer science and biology. The biology professor wanted to underscore the importance of the discovery of DNA, or perhaps just had a chip on his shoulder; he took pleasure in baiting the minority of computer science students in the class by telling us that the invention of the computer is nothing, completely irrelevant, compared to the enormity of the discovery of DNA and genetic engineering, and that we should all stop studying computers and study biology instead. Rather than let him irritate me, I only smiled - because it is laughable that he would say this, while not realizing his whole syllabus relies on a third technology which eclipses both in significance: the web.
So what do we do with the greatest invention of all time? What are we using the internet for? If you have Ruby (a computer language) installed on your computer, I can save you the trouble of reading 999 reddit links to find out. This Ruby program (from my Downloads page) rips every current reddit title and runs a modified Markov learning algorithm on the titles. Then it uses what it learned to generate sample titles in reddit "style". What results is a hilarious mis-mash. I'll leave you with a few of the most interesting tidbits it generated (these are machine-generated; I didn't make these up):
Giant Mackerel Gets Married
Giant Mackerel Gets Even Creepier
Baby Marmosets Are To Analyze Software Patents
Cockatoo Guarding Chocolate Good, Morbid Obesity Soaring
Even worse than one peep jousting: Two peeps
Anarchy On Sudafed Claims First Free Gps
Extreme D.I.Y. - Solitaire
Doomsday For How Blogging Can Die Of Education
Reddit Users To Prevent Manatee Deaths? Prevent Manatees!
Ask Reddit: what modern world?
I Read Reddit Users Are Small. [pic] This Pathetic Story
Anatomy Of Yoda [photo]
sex-mad? Mabye. Obsessed? With Chainsaw Could possibly be stopped
Grindhouse: Good movie or faked orgasm?
Wednesday, April 25, 2007
Homeless in the 21st Century
It's now the interim period between the end of my NASA internship and the beginning of my summer research fellowship. I find myself - once again - homeless, while I wait for my housing to become available for the next semester.
As a college student, this happens to me three times a year; the transitions of the fall, spring, and summer semesters drive also the availability of housing for college students. Whether it be the dorm kicking everyone out for two weeks between semesters, or campus apartments unwilling to extend a summer lease past August 9th when I can't move into fall housing until August 23rd, the gaps between semesters are not a vacation but a hardship for those of us who don't have anywhere else to go during those times. My parents would be willing to take me in, but they have such a small house that there isn't a bed for me to sleep on there. So in the interim weeks between semesters I become a modern gypsy, taking my car from place to place and staying with friends until my welcome runs out.
This semester is not too bad. One of my professors has kindly allowed me the use of his spare room, and we get along well. I am experiencing no physical hardship - only the mental anguish of trying to define home and coming up empty-handed. I have been provided a house to stay in, and hospitality, but it is not my home. In my heart, home is with my fiancee Melissa, but neither of us has our own place. Last semester, Melissa's week consisted of bouncing from Weatherford, to Edmond, to Norman, depending on where she needed to be for college or work. When we are married we'll have a home together, but for now we make do as best we can.
I drove to Weatherford today, but my professor happened to be out, and I felt funny about going into his house alone. So I picked somewhere far away and drove there until he would get back. It's times like this that my car means more to me than just transportation. It represents freedom, and a much needed piece of the familiar in unfamiliar lands. As long as I am rushing 78 mph down an unending stretch of freeway at least I am somewhere, doing something, which is infinitely better than nothing, nowhere.
As a college student, this happens to me three times a year; the transitions of the fall, spring, and summer semesters drive also the availability of housing for college students. Whether it be the dorm kicking everyone out for two weeks between semesters, or campus apartments unwilling to extend a summer lease past August 9th when I can't move into fall housing until August 23rd, the gaps between semesters are not a vacation but a hardship for those of us who don't have anywhere else to go during those times. My parents would be willing to take me in, but they have such a small house that there isn't a bed for me to sleep on there. So in the interim weeks between semesters I become a modern gypsy, taking my car from place to place and staying with friends until my welcome runs out.
This semester is not too bad. One of my professors has kindly allowed me the use of his spare room, and we get along well. I am experiencing no physical hardship - only the mental anguish of trying to define home and coming up empty-handed. I have been provided a house to stay in, and hospitality, but it is not my home. In my heart, home is with my fiancee Melissa, but neither of us has our own place. Last semester, Melissa's week consisted of bouncing from Weatherford, to Edmond, to Norman, depending on where she needed to be for college or work. When we are married we'll have a home together, but for now we make do as best we can.
I drove to Weatherford today, but my professor happened to be out, and I felt funny about going into his house alone. So I picked somewhere far away and drove there until he would get back. It's times like this that my car means more to me than just transportation. It represents freedom, and a much needed piece of the familiar in unfamiliar lands. As long as I am rushing 78 mph down an unending stretch of freeway at least I am somewhere, doing something, which is infinitely better than nothing, nowhere.
Friday, April 20, 2007
Gun Threat in the NASA Building I Work In!
Hot on the heels of the VT shooting, everyone is on edge. Now there has been a report of a gunman at the building I work in on the Johnson Space Center!
On Friday mornings I do a teleconference meeting from home. Today the conference ran late and made me late coming in to work. I was just about to head down to the Johnson Space Center to go in to building 44 to get the paperwork I need to check out, when I got a call from a coworker. He said there was a man with a gun and security is on lock down!
I don't really have much more information than that yet, and what more I have heard so far is unconfirmed so I will not start any rumors with it. But it's just crazy; I mean, I work there! Let me tell you that Building 44 is not the place I would have in a million years expected to see this happen. It's been the most stress-free work place I've seen, and I can't imagine what would push someone over the edge.
Update: They have helicopters showing the building on the news on CNN. Unreal.
On Friday mornings I do a teleconference meeting from home. Today the conference ran late and made me late coming in to work. I was just about to head down to the Johnson Space Center to go in to building 44 to get the paperwork I need to check out, when I got a call from a coworker. He said there was a man with a gun and security is on lock down!
I don't really have much more information than that yet, and what more I have heard so far is unconfirmed so I will not start any rumors with it. But it's just crazy; I mean, I work there! Let me tell you that Building 44 is not the place I would have in a million years expected to see this happen. It's been the most stress-free work place I've seen, and I can't imagine what would push someone over the edge.
Update: They have helicopters showing the building on the news on CNN. Unreal.
Last Day of My NASA Internship
It's been a busy week since my last blog entry. Fear not, dear reader(s), I have not abandoned you. I'll have more time to write next week, including some entries about how my internship went. And will they ask me to come back? I'll find out next fall.
Friday, April 13, 2007
Reading: Zero
My sister mailed me the book Zero: The Biography of a Dangerous Idea. The book has already used four of my favorite words, and that's just the title! Zero is my favorite number; I like biographies, and I like dangerous ideas best of all. I can't wait to start reading it.
Thanks Rose!
Thanks Rose!
Start Your Own OS Project in 30 Minutes
Have you ever dreamed of writing your own operating system? The first step is to get a kernel loaded. It's easier than you think. I'll show you how.
If you took an operating systems course in college, unless you went somewhere like Berkeley or Vrije in Amersterdam, it's likely that there was not a professor in your whole university who knew how to write an OS kernel from scratch and get it to boot by itself. Look, they're only human, ok? - most programmers wouldn't know how to do it. But it also means you came away from that class vaguely dissatisfied. Because if you take an operating systems class and never write your own simple kernel, it's like taking a driver's-ed course without ever getting to sit behind the wheel.
It's time we changed how we teach operating systems. Let's get started.
The first hurdle to overcome is getting the BIOS to boot your code. There are 512If you took an operating systems course in college, unless you went somewhere like Berkeley or Vrije in Amersterdam, it's likely that there was not a professor in your whole university who knew how to write an OS kernel from scratch and get it to boot by itself. Look, they're only human, ok? - most programmers wouldn't know how to do it. But it also means you came away from that class vaguely dissatisfied. Because if you take an operating systems class and never write your own simple kernel, it's like taking a driver's-ed course without ever getting to sit behind the wheel.
It's time we changed how we teach operating systems. Let's get started.
bytes of code at the beginning of a disk, which the BIOS loads and executes. This is called the boot sector, and it is the responsibility of this incredibly tiny program to load other code and finally the actual kernel.
My first intention was to write one of these boot sector programs, because it would be a way to get code running on the machine completely by itself. There is just enough space in 512 bytes to write a very simple program. But that doesn't sound very useful, and besides, it's been done before.
So I wanted to contribute something new. Besides my idea of putting a toy operating system in the boot sector not being original, there is already a perfectly good boot loader program available. It's called GRUB. Why not put GRUB in your boot sector instead of custom code, and use GRUB to bootstrap your prototype OS? Suddenly what would have been weeks slogging through nasty bootstrap code isn't necessary. Things are looking up.
GRUB stands for GRand Unified Bootloader. If you dual-boot Linux and Windows, you may know GRUB can load your favorite operating systems, but did you know there's another reason it's called Unified? The people who developed GRUB looked at all these operating systems floating around; they all need many of the same things to happen as they boot, but each implements their own proprietary boot loader. They said, "What if there were a universal standard for booting an operating system? Then we could write one really good boot loader that would suffice for any operating system." They did.
What they came up with was the Multiboot Specification. It's a magic header that you include in a program to tell GRUB the information it needs to know to load your kernel. GRUB is smart enough to read a file system, so you don't even have to place your kernel in specific sectors or make some kind of wacky system file. Instead, all you have to do is compile a program as you would normally, and you can place the ordinary program file in any directory on the disk.
In an article I read recently, Julio Vidal describes how he made NetBSD Multiboot-Compliant. The article includes a detailed description of the boot process, and is an interesting read all around. Most importantly for us, though, the article links to some example code, from the GRUB documentation, which shows how to make a minimal kernel that is multiboot-compliant.
You can find that example code here: http://cvs.savannah.gnu.org/viewcvs/grub/grub/docs/#dirlist
Don't worry, you don't need all the files listed on that page - just three of them. You need boot.S, kernel.c, and multiboot.h. In order to illustrate the proof of concept (that, once loaded, I can run any code I want), I made a slight modification to the files which simply displays my own custom message after the kernel boots. You may want to work with the modified version in order to see where to put your own code, and because it is the version I used for these instructions, although the unmodified files should work too. You can get them from my downloads page: boot.S | kernel.c | multiboot.h
Now that you have the code, you need to compile it. For this, you will need to be running Linux. You'll also need Linux for the later steps involving GRUB, so you might as well get it now if you don't have it already. I recommend Ubuntu. Ubuntu comes on a Live CD, so you can run it from the CD without even having to install it. You can also install Ubutunu on an external hard drive or even a USB memory stick. (I will be posting another blog entry soon explaining how I did that.)
Compiling the code is the second major hurdle to cross. Most C programs link in libraries for things such as I/O and debugging. These C libraries in turn depend on an OS, or more specifically, they rely on making calls to the kernel of an OS. Well hold on a minute - we haven't got a kernel yet; that's what we're trying to make. Not only can we not rely on the the standard libraries, we have to make sure the compiler doesn't sneak external references in anyway (which it will try to do if you let it). So we have to use special compiler options to force the compiler to generate code that stands alone and has no external dependencies.
Open a terminal in Linux and type:
cd /whatever/directory/you/have/the/code/in
Then use this command (all on one line) to compile it:
gcc boot.S kernel.c -o mbs-kernel -ffreestanding -nostdlib -nostartfiles -fno-stack-protector
Explanation:
This produces the file "mbs-kernel" which contains our new kernel.
You may need to be root to do the rest of the steps. To switch to superuser mode so that you can run all the commands, type:
sudo su
...and provide the root password when it asks.
Next we need to install grub to a floppy disk so that we can boot from it. It is possible to use a USB key instead, but I haven't figured out how to do that yet. So we'll use a floppy disk for this example.*
Open a terminal and type the following command:
grub --batch --device-map=/dev/null <<EOF
The above command starts GRUB in batch mode. Insert a formatted floppy disk and type these commands to install GRUB to it:
device (fd0) /dev/fd0
root (fd0)
setup (fd0)
quit
EOF
If you are using a USB external floppy (what I was using), then the device will be different. In that case, do not type the above, but use this set of commands instead:
device (fd0) /dev/sdb
root (fd0)
setup (fd0)
quit
EOF
USB floppy drive devices show up as /dev/sda, /dev/sdb, etc. Mine was sdb because I also have an internal SATA hard drive, and that occupies /dev/sda, so the floppy became /dev/sdb. If you have an ordinary IDE hard drive, then a USB floppy might be /dev/sda. How can you tell which one is your floppy disk? What I did was I used the Gnome file manager to mount the floppy disk (by double clicking its icon). Once I could see the contents of the floppy disk in the GUI file manager, I knew it had to be mounted. Then I opened up a terminal and typed:
mount -l
That's a lowercase "el". It caused mount to list all the mounted file systems - I looked at the one that matched where my floppy was mounted and the device given was the device I used for grub. However, if I recall, the drive has to actually be not mounted for the GRUB install to work. So after you mount the disk to find out its device file, be sure to right-click on it in the file manager and select "unmount" before you attempt to do the GRUB install.
Now that we have GRUB installed, we need to put our compiled kernel onto the disk. Use the file manager to mount the floppy drive and copy the file mbs-kernel to the /boot directory of the floppy disk.
*Note: If you already have GRUB on your hard drive, you don't necessarily have to have a boot floppy at all for this exercise. If you are comfortable with the idea of changing the boot configuration of your computer, you can safely skip the GRUB floppy install steps above, and place your homemade kernel directly in your /boot directory on your hard drive next to your Linux kernel. When you boot, you can choose whether to load Linux or your own kernel.
The last step is to modify the menu.lst file to make an entry for your kernel. If you have put your kernel on a floppy disk, you should be able to go to/boot/grub and double click on menu.lst in the file manager, which will open up gedit and let you edit the file. If you are going to run the kernel off your hard drive, you will find that gedit cannot save menu.lst if you opened it by double clicking. This is just a security precaution by Linux - the file manager didn't run gedit as root when you double clicked the file, so it can't write to it to save your changes. No matter - you can easily start gedit from the superuser prompt, and then it will have root priviledges. Just type:
gedit /boot/grub/menu.lst
Or, if you're back at the $ prompt instead of #, type:
sudo gedit /boot/grub/menu.lst
Menu.lst is simply a script that GRUB reads to know what boot menu options to show and how to execute them. At the very bottom of the file, if you are using a floppy, add the lines:
title Multiboot Kernel Example
root (fd0)
kernel /boot/mbs-kernel
If you want to boot your kernel directly from your hard drive, put the file mbs-kernel into your /boot directory on your hard drive, and add this to the bottom of your menu.lst file in /boot/grub/menu.lst:
title Multiboot Kernel Example
root (hd0)
kernel /boot/mbs-kernel
Once you have prepared the disk, reboot your computer. If you are booting from a floppy, make sure your BIOS is set to be able to boot from the floppy drive. Some computers have a key you can press to get a boot menu and choose manually. GRUB should start up when the computer boots. Choose the Multiboot Kernel Example, and GRUB will start your kernel. If you were successful, you should see either:
My first intention was to write one of these boot sector programs, because it would be a way to get code running on the machine completely by itself. There is just enough space in 512 bytes to write a very simple program. But that doesn't sound very useful, and besides, it's been done before.
So I wanted to contribute something new. Besides my idea of putting a toy operating system in the boot sector not being original, there is already a perfectly good boot loader program available. It's called GRUB. Why not put GRUB in your boot sector instead of custom code, and use GRUB to bootstrap your prototype OS? Suddenly what would have been weeks slogging through nasty bootstrap code isn't necessary. Things are looking up.
GRUB stands for GRand Unified Bootloader. If you dual-boot Linux and Windows, you may know GRUB can load your favorite operating systems, but did you know there's another reason it's called Unified? The people who developed GRUB looked at all these operating systems floating around; they all need many of the same things to happen as they boot, but each implements their own proprietary boot loader. They said, "What if there were a universal standard for booting an operating system? Then we could write one really good boot loader that would suffice for any operating system." They did.
What they came up with was the Multiboot Specification. It's a magic header that you include in a program to tell GRUB the information it needs to know to load your kernel. GRUB is smart enough to read a file system, so you don't even have to place your kernel in specific sectors or make some kind of wacky system file. Instead, all you have to do is compile a program as you would normally, and you can place the ordinary program file in any directory on the disk.
In an article I read recently, Julio Vidal describes how he made NetBSD Multiboot-Compliant. The article includes a detailed description of the boot process, and is an interesting read all around. Most importantly for us, though, the article links to some example code, from the GRUB documentation, which shows how to make a minimal kernel that is multiboot-compliant.
You can find that example code here: http://cvs.savannah.gnu.org/viewcvs/grub/grub/docs/#dirlist
Don't worry, you don't need all the files listed on that page - just three of them. You need boot.S, kernel.c, and multiboot.h. In order to illustrate the proof of concept (that, once loaded, I can run any code I want), I made a slight modification to the files which simply displays my own custom message after the kernel boots. You may want to work with the modified version in order to see where to put your own code, and because it is the version I used for these instructions, although the unmodified files should work too. You can get them from my downloads page: boot.S | kernel.c | multiboot.h
Now that you have the code, you need to compile it. For this, you will need to be running Linux. You'll also need Linux for the later steps involving GRUB, so you might as well get it now if you don't have it already. I recommend Ubuntu. Ubuntu comes on a Live CD, so you can run it from the CD without even having to install it. You can also install Ubutunu on an external hard drive or even a USB memory stick. (I will be posting another blog entry soon explaining how I did that.)
Compiling the code is the second major hurdle to cross. Most C programs link in libraries for things such as I/O and debugging. These C libraries in turn depend on an OS, or more specifically, they rely on making calls to the kernel of an OS. Well hold on a minute - we haven't got a kernel yet; that's what we're trying to make. Not only can we not rely on the the standard libraries, we have to make sure the compiler doesn't sneak external references in anyway (which it will try to do if you let it). So we have to use special compiler options to force the compiler to generate code that stands alone and has no external dependencies.
Open a terminal in Linux and type:
cd /whatever/directory/you/have/the/code/in
Then use this command (all on one line) to compile it:
gcc boot.S kernel.c -o mbs-kernel -ffreestanding -nostdlib -nostartfiles -fno-stack-protector
Explanation:
- -o mbs-kernel: names the output file
- -ffreestanding: means the resulting program should have no dependencies
- -nostdlib: tells the linker not to bring in the C standard library, which won't work in the kernel
- -nostartfiles: tells the linker not to bring in any C startup code
- -fno-stack-protector: is a fix for the error "undefined reference to '__stack_chk_fail'"
This produces the file "mbs-kernel" which contains our new kernel.
You may need to be root to do the rest of the steps. To switch to superuser mode so that you can run all the commands, type:
sudo su
...and provide the root password when it asks.
Next we need to install grub to a floppy disk so that we can boot from it. It is possible to use a USB key instead, but I haven't figured out how to do that yet. So we'll use a floppy disk for this example.*
Open a terminal and type the following command:
grub --batch --device-map=/dev/null <<EOF
The above command starts GRUB in batch mode. Insert a formatted floppy disk and type these commands to install GRUB to it:
device (fd0) /dev/fd0
root (fd0)
setup (fd0)
quit
EOF
If you are using a USB external floppy (what I was using), then the device will be different. In that case, do not type the above, but use this set of commands instead:
device (fd0) /dev/sdb
root (fd0)
setup (fd0)
quit
EOF
USB floppy drive devices show up as /dev/sda, /dev/sdb, etc. Mine was sdb because I also have an internal SATA hard drive, and that occupies /dev/sda, so the floppy became /dev/sdb. If you have an ordinary IDE hard drive, then a USB floppy might be /dev/sda. How can you tell which one is your floppy disk? What I did was I used the Gnome file manager to mount the floppy disk (by double clicking its icon). Once I could see the contents of the floppy disk in the GUI file manager, I knew it had to be mounted. Then I opened up a terminal and typed:
mount -l
That's a lowercase "el". It caused mount to list all the mounted file systems - I looked at the one that matched where my floppy was mounted and the device given was the device I used for grub. However, if I recall, the drive has to actually be not mounted for the GRUB install to work. So after you mount the disk to find out its device file, be sure to right-click on it in the file manager and select "unmount" before you attempt to do the GRUB install.
Now that we have GRUB installed, we need to put our compiled kernel onto the disk. Use the file manager to mount the floppy drive and copy the file mbs-kernel to the /boot directory of the floppy disk.
*Note: If you already have GRUB on your hard drive, you don't necessarily have to have a boot floppy at all for this exercise. If you are comfortable with the idea of changing the boot configuration of your computer, you can safely skip the GRUB floppy install steps above, and place your homemade kernel directly in your /boot directory on your hard drive next to your Linux kernel. When you boot, you can choose whether to load Linux or your own kernel.
The last step is to modify the menu.lst file to make an entry for your kernel. If you have put your kernel on a floppy disk, you should be able to go to
- A screen full of gobbledygook if you booted the default GNU example code
- A friendly message if you booted my version
If you get a message from GRUB that says "Error 28: Selected item cannot fit into memory," that error message is badly worded. It really means, "Default load address is too high." I got the error on a machine with 32 MB of RAM, but not on one with 256 MB. This problem could be fixed by instructing GCC to compile the program for a lower memory address. Anyone know how to do that?
In any case, as soon as you get the kernel to boot, you should immediately boot back into Linux, delete my code changes, and add your own code instead. This is your kernel.
If you want to go further, here are some resources:
http://www.cs.utah.edu/flux/oskit/
http://www.osdev.org/wiki/Main_Page
Remember, you are starting with a blank slate. You won't have a filesystem driver, I/O, anything, until you put them into your OS. You are starting at the bare metal. One way of looking at it is that you have nothing holding up your code.
Another way of looking at it, is that you have nothing holding you back.
Happy hacking.
In any case, as soon as you get the kernel to boot, you should immediately boot back into Linux, delete my code changes, and add your own code instead. This is your kernel.
If you want to go further, here are some resources:
http://www.cs.utah.edu/flux/oskit/
http://www.osdev.org/wiki/Main_Page
Remember, you are starting with a blank slate. You won't have a filesystem driver, I/O, anything, until you put them into your OS. You are starting at the bare metal. One way of looking at it is that you have nothing holding up your code.
Another way of looking at it, is that you have nothing holding you back.
Happy hacking.
Subscribe to:
Posts (Atom)