2015-04-16

The bootloader, chained


OK, I guess it's high time I wrote about this because it's a bitch to collect all the different bits.

Current u-boot does support Snow board out of the box.
  • it can boot zImage
  • supports simplefb
  • you can hack it easily.
Installing u-boot in the SPI NOR flash has its issues
  • no support for accessing the flash under Linux out of the box (ALARM has some flashroom-google worth looking at and u-boot can access the flash).
  • part of the flash with the bootloader is readonly unless you take apart the chromebook and remove write protection
  • if you have no means accessing the flash offline from other system you might render your chromebook unbootable if you write wrong bootloader image in the flash
As workaround you can chainload u-boot packaged as Linux kernel from the existing u-boot in the onboard flash. There is a guide which kind of does not exactly apply to current u-boot. First you will need to build an u-boot for snow.
git clone git://git.denx.de/u-boot.git
cd u-boot
The u-boot preinstalled in flash has a bug that clobbers some memory so you need to change load address of u-boot using this patch. Since the patch does not apply to mainline you should apply it by hand. You can see that CONFIG_SYS_TEXT_BASE is set in include/configs/exynos5250-common.h so change it to 0x42400000
$ git grep -i 0x43e00000 | grep exynos
include/configs/exynos5250-common.h:#define CONFIG_SYS_TEXT_BASE        0x43E00000
$ vi include/configs/exynos5250-common.h
$ git diff
diff --git a/include/configs/exynos5250-common.h b/include/configs/exynos5250-common.h
index ae0e5ff..4226043 100644
--- a/include/configs/exynos5250-common.h
+++ b/include/configs/exynos5250-common.h
@@ -14,7 +14,7 @@
 #define CONFIG_EXYNOS5250

 #define CONFIG_SYS_SDRAM_BASE          0x40000000
-#define CONFIG_SYS_TEXT_BASE           0x43E00000
+#define CONFIG_SYS_TEXT_BASE           0x42400000

 /* MACH_TYPE_SMDK5250 macro will be removed once added to mach-types */
 #define MACH_TYPE_SMDK5250             3774
$ make ARCH=arm snow_defconfig
$ make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- -j6
This should produce  u-boot-dtb.bin To make this image bootable as linux kernel you first package it as uImage with load address as defined in CONFIG_SYS_TEXT_BASE. This uImage can then be signed and loaded as the kernel uImage. Use a dummy text file to make the sign tool happy that all pieces have been provided.
mkimage -A arm -O linux -T kernel -C none -a 0x42400000 -e 0x42400000 -n "u-boot" -d u-boot.dtb.bin u-boot-dtb.uImage
echo blah > dummy.txt
vbutil_kernel \
--pack u-boot.part \
--keyblock /usr/share/vboot/devkeys/kernel.keyblock \
--signprivate /usr/share/vboot/devkeys/kernel_data_key.vbprivk \
--version 1 \
--vmlinuz u-boot-dtb.uImage \
--config dummy.txt \
--arch arm
Now writing u-boot.part to a kernel partition should produce a bootable medium.


2013-08-19

The system

So, the Chromebook comes with ChromeOS preinstalled.

It's a cool system but I prefer something that has a terminal and  ssh as first class application at the very least. The terminal in ChromeOS is ridiculous.

The internal eMMC is not very large but it's quite fast and you can use a SDXC card or USB thumb drive for further storage.

To install a sane system you will first need to switch to developer mode.

Hold down the ESC and Refresh keys and poke the Power button. To enter Dev-mode you first invoke Recovery, and at the Recovery screen press Ctrl-D (there’s no prompt – you have to know to do it). It will ask you to confirm, then reboot into dev-mode.
You’ll still have to run “crossystem dev_boot_usb=1” and reboot once to boot from USB drives with Ctrl-U.
To run that command you boot the device in dev mode. press something like Ctrl-Alt-T to enter a shell, then type shell to really enter a shell. The SD card slot counts as USB, ew.

I prefer to first install a new system on removable media so that it's easy to dissect when it turns out broken and still leaves you with a working system in the device. The Chromebook allows booting from a SD card so you can keep ChromeOS on disk while playing with GNU/Linux.

To install you will obviously need a SD card, a SD card reader to use with your PC (you can possibly install the image directly on the Chromebook but it's easier to do on a sane system with command history, etc.

On your PC you will need ARM crosscompiler - eg. from emdebian you can get arm-linux-gnueabihf-gcc (Debian 4.7.2-5) 4.7.2.

To actually use the compiler symlink arm-linux-gnueabihf-gcc-4.7 as arm-linux-gnueabihf-gcc somewhere in your path.

You will also need debootstrap, qemu-user-static, u-boot-tools, device-tree-compiler, vboot-utils, vboot-kernel-utils, gdisk and cgpt. device-tree-compiler in Debian stable is version 1.1 which is too old. device-tree-compiler 1.3 from testing should work.

A Chromebook kernel source can be found here:  https://git.chromium.org/chromiumos/third_party/kernel.git (the chromeos-3.4 branch). Using GNU/Linux with this kernel is unsupported. If you experience bugs that don't happen with chromium you are on your own.  There are some nice guides for building the kernel.

To compile run something like this:
chromeos/scripts/prepareconfig chromeos-exynos5
Edit the .config - you will want to disable LSM because it prevents you from loading modules:
CONFIG_SECURITY_CHROMIUMOS=n
The Debian compiler does not support  -fstack-protector-strong so you will want to patch out the -strong part.

You can patch the kernel config to allow building middle mouse emulation on non-mac:
CONFIG_MACINTOSH_DRIVERS=y
CONFIG_MAC_EMUMOUSEBTN=y
 The config is pretty minimal so make sure you have drivers for any external hardware you plan to connect to your Chromebook.
make ARCH=arm dtbs
make ARCH=arm CROSS_COMPILE="arm-linux-gnueabihf-" -j6
mkimage -f kernel.its kernel.itb
echo "console=tty1 debug root=/dev/mmcblk1p3 rw rootwait" > config.txt
vbutil_kernel --pack kernel-image \
      --keyblock /usr/share/vboot/devkeys/kernel.keyblock \
      --version 1 \
      --signprivate \
/usr/share/vboot/devkeys/kernel_data_key.vbprivk \
      --config config.txt --vmlinuz kernel.itb --arch arm
You will need WiFi firmware (IIRC the sd8797_uapsta.bin).

You can easily build an arm chroot and install/set up packages you will need on your fresh Linux system.You will need at least one kernel partition 4MB in size (the ChromeOS kernel is about 3.6MB) and a root partition for your Linux installation. You can include more than one kernel partition so that you have a backup and can recover easily when you install a broken kernel.

The ChromeOS loader uses GPT and is particular about flags and partition types so you will need a GPT partitioner that can set them. gdisk can set partition types properly - chrome kernel is type 7f00. The bootloader will not load a kernel from partition with different type. To match the mmcblk1p3 above in the kernel parameters make two kernel partitions followed by an ext4 partition which will be mounted as root. Linux filesystem is supposed to be type 8300 which is the default in gdisk.

To copy the kernel image use dd if=kernel-image of=/dev/<your card device>1 bs=4M (size of your kernel partition). Copy the arm chroot to your card using tar or whatever. Don't forget to add the kernel modules and firmware.

To mark the kernel partition as bootable you need to set flags with cgpt or other tool that can manipulate them. Example:
cgpt add -i 1 -S 1 -T 5 -P 10 -l KERN-A /dev/<your card device>

cgpt add -i 1 -S 1 -P 10 /dev/<your card device>
 -i gives the partition number, -S is 'succesful' and partitions with this flag zero are not bootable. -P gives priority. With multiple bootable  partitions the one with higher P is selected.  -l is label which can be used to name the partition but is not used by bootloader. T is number of tries but needs not be set - T 0 partitions are also bootable. Note cgpt is a partitioner and operates on whole disk. The partition it opertaes on is selected with -i. Some guides give wrong instructions showing cgpt used on a partition.

2013-08-16

The hardware



Samsung Chromebook
Samsung Chromebook (picture by Google)
You can see specs on the Google site as well as Samsung site.

In short it's about 12" based on Exynos 5 SoC. It has somewhat passable 1366x768 (quite odd aspect) LED screen, some eMMC for storage, USB2, USB3, HDMI, SDXC, audio connectors. The SATA of the SoC is sadly unused. Keyboard is very limited and touchpad comes with those odd buttons which are actiavated by depressing the whole thing at the near edge. For connectivity you get WiFi and optional 3G. USB Ethernet is supported (eg. Axago, Apple adaptors worked for me).

Samsung Chromebook keyboard
Samsung Chromebook keyboard detail (picture by Samsung)
Note the magnifier glass key which is actually a Windows key