Setting up the bootable SD card

From Orange Pi
Jump to: navigation, search

Introduction

This page describes how to create a bootable SD card. Depending on how the SD card is connected, the location to write data can be different. Throughout this document ${card} refers to the SD card and ${p} to the partition if any. If the SD card is connected via a USB adapter, Linux will know it for example as /dev/sdX (X represents the b or c or d …). Please notice that this device would be different based on numerous factors, so when you are not sure, use fdisk command to check it. If connected via a SD slot on a device, linux will know it as /dev/mmcblk0 (or mmcblk1, mmcblk2 depending on which mmc slot is used).
Data is either stored raw on the SD card or in a partition. If ${p} is used then the appropiate partition should be used. Also this differs for USB adapters or mmc controllers. When using an USB adapter, ${p} will be 1, 2, 3 etc so the resulting device is /dev/sdX1. Using an mmc controller, this would be p1, p2, p3 etc so the resulting device is /dev/mmcblk0p1.
To summarize, ${card} and ${card}${p}1 mean /dev/sdX and /dev/sdX1 on a USB connected SD card, and /dev/mmcblk0, /dev/mmcblk0p1 on an mmc controller connected device.
If the SD card is physically removed but then inserted into/connected to another slot or port of the PC, the device nodes can change again and be different, so be aware of this and take it into account.
It is assumed that you have already got files such as u-boot-sunxi-with-spl.bin, uImage and script.bin. (See the previous section in this Wiki “1. Building u-boot, script.bin and linux-kernel” for how to do this.)

SD Card Layout

SD Card Layout
start size usage
0 8KB Unused, available for partition table etc.
8 24KB Initial SPL loader
32 512KB u-boot
544 128KB environment
672 352KB reserved
1024 - Free for partitions


Partitioning and formatting

First identify the device of the card:

sudo fdisk -l

This wll list all storage device connected to your PC. Find out the SD card device node.
Assume the SD card is connected via USB and is sdb

export card=/dev/sdb
export p=""

If the SD card is connected via mmc and is mmcblk0

export card=/dev/mmcblk0
export p=p


First use the "umount" command to unmount all the partitions, and then use "fdisk" command to partition the SD card:

sudo fdisk ${card}

Use command o to delete all partitions, and then use command n to add new partitions.
Add the first partition

Command (m for help): n                                 # Type n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p):                                     # Press Enter Key       
Using default response p
Partition number (1-4, default 1):                      # Press Enter Key
Using default value 1
First sector (2048-15523839, default 2048):             # Press Enter Key
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-15523839, default 15523839): +20M      # Type +20M


Add the second partition

Command (m for help): n
Partition type:                                           # Press Enter Key
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): 
Using default response p
Partition number (1-4, default 2):                        # Press Enter Key
Using default value 2
First sector (43008-15523839, default 43008): 
Using default value 43008
Last sector, +sectors or +size{K,M,G} (43008-15523839, default 15523839): # Press Enter Key 
Using default value 15523839


Write the partition table into the SD card:

Command (m for help): w                                   # Type w
The partition table has been altered!


Format the partition:

sudo mkfs.vfat ${card}${p}1
sudo mkfs.ext4 ${card}${p}2


Bootloader

To be on safe side erase the first part of your SD card:

sudo dd if=/dev/zero of=${card} bs=1k count=1023 seek=1

Burn the bootloader into the SD card:

sudo dd if=${u-boot-dir}/u-boot-sunxi-with-spl.bin of=${card} bs=1024 seek=8

where ${u-boot-dir} represents the directory in which the u-boot-sunxi-with-spl.bin is.

Boot Script

Mount the first partition of the SD card:

sudo mount ${card}${p}1 /mnt

You can use the boot.src or uEnv.txt as the boot script, just choose one.
Boot.src
Create boot.cmd in /mnt with the following content:

setenv bootargs console=ttyS0,115200 noinitrd 
disp.screen0_output_mode=EDID:1280x720p50 init=/init
root=/dev/mmcblk0p2 rootwait panic=10 ${extra}
fatload mmc 0 0x43000000 script.bin 
fatload mmc 0 0x48000000 uImage 
bootm 0x48000000

then, generate boot.src:

sudo mkimage -C none -A arm -T script -d /mnt/boot.cmd /mnt/boot.scr


uEnv.txt
Create uEnv.txt in /mnt with the following content:

bootargs=console=ttyS0,115200 disp.screen0_output_mode=EDID:1024x768p50 hdmi
aload_script=fatload mmc 0 0x43000000 script.bin;
aload_kernel=fatload mmc 0 0x48000000 uImage;bootm 0x48000000;
uenvcmd=run aload_script aload_kernel


Kernel

Copy the uImage and script.bin files into the first partition:

sudo cp ${uImage_dir}/uImage /mnt
sudo cp ${script.bin_dir}/script.bin /mnt

where ${uImage_dir} represents the directory which the uImage file is in, while
${script.bin_dir} represents the directory which the script.bin file is in.

After copying all the files into the first partition, unmount that first partition:

sudo umount /mnt


At this point, you could insert your SD card into the Orange Pi, and this would boot to the kernel normally. But as there is no root file system in the second partition, the board will just keep restarting again and again. For the next step, please refer to the next section entitled Setting up the Linux distribution root file system.

Reference

1. http://sunxi.org/Bootable_SD_card#Cleaning