Where is the bootloader stored - in ROM, RAM, or elsewhere?

ROM is a separate chip from the RAM. It does not require power to retain its contents, and originally could not be modified by any means, but came hard wired from the factory. Later PROM, or Programmable Read Only Memory replaced true ROM. These chips came blank from the factory, and could be written to once using a special procedure that essentially burned out bits of the chip causing their state to change. This was then replaced with EPROM, or Eraseable Programmable Memory. These chips had a little window on them and if you shined ultraviolet light into them, could be erased, allowing them to be programmed again. These were then replaced with EEPROM, or Electrically Erasable Programmable Memory. These chips have a special software procedure to erase them so they can be reprogrammed. ROM generally is still used to refer to all of these types generically.

The motherboard has some type of ROM chip that holds the firmware, which in PC parlance is usually called the BIOS, or Basic Input Output System, though it is being replaced these days with EFI firmware. This is the software that the CPU first starts executing at power on. All firmware performs initialization of the hardware, typically provides some diagnostic output, and provides a way for the user to configure the hardware, then locates and loads the boot loader, which in turn locates and loads the OS.

With PC BIOS, it simply loads and executes the first sector off the disk it decides to boot from, which typically is the first hard disk detected. By convention the first sector of a hard disk, called the Master Boot Record, contains a DOS partition table listing the locations of the partitions on the disk, and and leaves some space for the boot loader. Ubuntu uses the GRUB boot loader, which places enough code in the MBR to load and execute /boot/grub/core.img. Normally a copy of this file is placed in the sectors following the MBR, but before the first partition, and that is actually what the MBR loads, since finding the location of /boot/grub/core.img is too difficult to do properly in the very limited space available in the MBR.

The grub core image contains the base grub code, plus any modules needed to access /boot/grub so that it can load additional modules there, and the grub config file that describes what operating systems can be booted, and where they can be found.

The EFI firmware used on Intel Macs and becoming available as a replacement to BIOS on most recent PC motherboards requires a dedicated partition that holds boot loader files, and the firmware is smart enough to find those files and load one instead of just loading and executing whatever is in the first sector of the disk.


ROM is not in Main memory:

ROM isn't a part of Main memory. It is a separate chip, which is most of the time built-in in a bigger IC. More example, Your PC can consists of several ROM. Those are built in your mother board.

Generally :

  • ROM are very small in memory size. These memory is non-volatile, in the sense, the program stored in ROM don't get erased with power shut down.
  • ROM are used to store permanent programs, which are very essential your hardware to perform correctly.
  • A typical example of ROM is BIOS chip. Where the program of very low level booting and initializing hardware are stored

You mentioned that, You read an article where author said, "ROM is a part of Main memory". This is confusing, because typically Main Memory refers to the memory of kinds which are volatile, such as RAM. But yes, If you use Main Memory term for the entire memory space of a PC, then ROM is a part of that memory space. You should note that, generally Main Memory exclude memory of kinds as ROM.

Where is Bootloader stored:

Modern system use two stage boot loading. In the first step a tiny program is loaded from a sector (called boot-sector more often) of Hard disk. This tiny program in turn load a program from some where in the disk, which is called bootloader. And finally bootloader loads the OS.

In term of Ubuntu system, the process is as following:

  1. After you turn on your PC, BIOS (which is stored in ROM) automatically runs and initializes various parts of PC hardware. Then it checks for a specific sector in the defined first Boot device (typically Hard disk). This sector is boot-sector, which is 512 bytes in size.

  2. The program in the boot-sector is loaded in memory (1st stage). This tiny program has information which programs should it load into memory next and where that program is located in Disk or Boot device. It loads that program. In Ubuntu, it is /boot/grub/core.img.

  3. In the 2nd stage, The OS-Loader, GRUB, loads Ubuntu by loading the kernel and initial ram disk into the memory and hands-over control to the kernel. Then the kernel runs and loads all of the necessary programs such as display manager, Gui's etc.

So, we can clearly say, that the bootloader is neither stored in ROM, nor in RAM, It is actually stored on Hard disk (or other Boot device, such as bootable CDROM, USB drives etc) , precisely speaking the first sector of the hard disk, which is of size 512 bytes and often referred to as the boot-sector. And this bootloader loads the OS-loader (in Ubuntu, it is grub) which also resides in the hard disk (i.e /boot/grub/ folder), and it's task is loading the OS (say, Ubuntu).

As a test, Remove the hard disk (and all other boot device) and try to boot. You can go up to the BIOS step, but after that step, you can't boot anything. Most probably the BIOS will say, "No Boot device Found" or "No Operating System found" or something similar.

Hope this answer will help.


For more information, you may want to visit these links:

  1. http://en.wikipedia.org/wiki/Read-only_memory
  2. http://en.wikipedia.org/wiki/Random-access_memory

An x86-compatible processor always starts in so-called "real" mode, which is a 16-bit mode with 1 megabyte of addressable memory available. From that address space, 640K are available for programs, and addresses above that are mapped to different devices.

For example, addresses starting from 0xA000:0x0000 are mapped to the video RAM, so, writing data there would actually write data to the video adapter's memory displaying pixels on the screen.

Similarly, BIOS ROM starts from 0xF000:0000 so the CPU, when powered on, simply starts executing instructions one by one starting from that pre-defined address. The BIOS ROM contains the initial program which starts by performing a "power-on self test", or POST. From Wikipedia:

The BIOS software is built into the PC, and is the first code run by a PC when powered on ('boot firmware'). When the PC starts up, the first job for the BIOS is the power-on self-test, which initializes and identifies system devices such as the CPU, RAM, video display card, keyboard and mouse, hard disk drive, optical disc drive and other hardware. The BIOS then locates boot loader software held on a peripheral device (designated as a 'boot device'), such as a hard disk or a CD/DVD, and loads and executes that software, giving it control of the PC.2 This process is known as booting, or booting up, which is short for bootstrapping.

The BIOS firmware is responsible for reading the first sector from the disk into memory and passing the control to a small program which is, again, located at a specific address there. The MBR bootloader may then start loading the OS directly (as was the case with MS-DOS) or to load "second stage", which is not limited to the confines of a single disk sector.

Bootloaders which use the multi-stage approach can be quite complex with text or graphical interface which allows user to select from which disk or partition to load an operating system.

So if, as Uri suggests, you're interested whether the Windows bootloader and GRUB can co exist together, the answer is: the actual MBR can only contain a single first-stage bootloader (come on, the whole sector is just 512 bytes), but the second stage of a bootloader may be able to "chain-load" operating systems from different partitions. Windows bootloader is only able to recognize and load Windows, while GRUB is able to load Linux or pass control to another bootloader stored in a volume boot record of one of the partitions, which allows to boot Windows or other OS. The latter process is called chain-loading.

When you install Ubuntu on a computer which has Windows on it, GRUB will be installed into MBR and you'll be able to boot both Ubuntu and Windows.

However, if Windows is installed after Ubuntu, GRUB will be replaced with Windows bootloader and you'll need to re-install GRUB to be able to boot Ubuntu again.