summaryrefslogtreecommitdiffstats
path: root/meta/classes/boot-directdisk.bbclass
diff options
context:
space:
mode:
Diffstat (limited to 'meta/classes/boot-directdisk.bbclass')
-rw-r--r--meta/classes/boot-directdisk.bbclass168
1 files changed, 168 insertions, 0 deletions
diff --git a/meta/classes/boot-directdisk.bbclass b/meta/classes/boot-directdisk.bbclass
new file mode 100644
index 0000000000..55357283ac
--- /dev/null
+++ b/meta/classes/boot-directdisk.bbclass
@@ -0,0 +1,168 @@
1# boot-directdisk.bbclass
2# (loosly based off bootimg.bbclass Copyright (C) 2004, Advanced Micro Devices, Inc.)
3#
4# Create an image which can be placed directly onto a harddisk using dd and then
5# booted.
6#
7# This uses syslinux. extlinux would have been nice but required the ext2/3
8# partition to be mounted. grub requires to run itself as part of the install
9# process.
10#
11# The end result is a 512 boot sector populated with an MBR and partition table
12# followed by an msdos fat16 partition containing syslinux and a linux kernel
13# completed by the ext2/3 rootfs.
14#
15# We have to push the msdos parition table size > 16MB so fat 16 is used as parted
16# won't touch fat12 partitions.
17
18# External variables needed
19
20# ${ROOTFS} - the rootfs image to incorporate
21
22do_bootdirectdisk[depends] += "dosfstools-native:do_populate_sysroot \
23 syslinux:do_populate_sysroot \
24 syslinux-native:do_populate_sysroot \
25 parted-native:do_populate_sysroot \
26 mtools-native:do_populate_sysroot "
27
28PACKAGES = " "
29EXCLUDE_FROM_WORLD = "1"
30
31BOOTDD_VOLUME_ID ?= "boot"
32BOOTDD_EXTRA_SPACE ?= "16384"
33
34EFI = "${@base_contains("MACHINE_FEATURES", "efi", "1", "0", d)}"
35EFI_CLASS = "${@base_contains("MACHINE_FEATURES", "efi", "grub-efi", "", d)}"
36
37# Include legacy boot if MACHINE_FEATURES includes "pcbios" or if it does not
38# contain "efi". This way legacy is supported by default if neither is
39# specified, maintaining the original behavior.
40def pcbios(d):
41 pcbios = base_contains("MACHINE_FEATURES", "pcbios", "1", "0", d)
42 if pcbios == "0":
43 pcbios = base_contains("MACHINE_FEATURES", "efi", "0", "1", d)
44 return pcbios
45
46def pcbios_class(d):
47 if d.getVar("PCBIOS", True) == "1":
48 return "syslinux"
49 return ""
50
51PCBIOS = "${@pcbios(d)}"
52PCBIOS_CLASS = "${@pcbios_class(d)}"
53
54inherit ${PCBIOS_CLASS}
55inherit ${EFI_CLASS}
56
57# Get the build_syslinux_cfg() function from the syslinux class
58
59AUTO_SYSLINUXCFG = "1"
60DISK_SIGNATURE ?= "${DISK_SIGNATURE_GENERATED}"
61SYSLINUX_ROOT ?= "root=/dev/sda2"
62SYSLINUX_TIMEOUT ?= "10"
63
64boot_direct_populate() {
65 dest=$1
66 install -d $dest
67
68 # Install bzImage, initrd, and rootfs.img in DEST for all loaders to use.
69 install -m 0644 ${STAGING_KERNEL_DIR}/bzImage $dest/vmlinuz
70
71 if [ -n "${INITRD}" ] && [ -s "${INITRD}" ]; then
72 install -m 0644 ${INITRD} $dest/initrd
73 fi
74
75}
76
77build_boot_dd() {
78 HDDDIR="${S}/hdd/boot"
79 HDDIMG="${S}/hdd.image"
80 IMAGE=${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}.hdddirect
81
82 boot_direct_populate $HDDDIR
83
84 if [ "${PCBIOS}" = "1" ]; then
85 syslinux_hddimg_populate $HDDDIR
86 fi
87 if [ "${EFI}" = "1" ]; then
88 grubefi_hddimg_populate $HDDDIR
89 fi
90
91 BLOCKS=`du -bks $HDDDIR | cut -f 1`
92 BLOCKS=`expr $BLOCKS + ${BOOTDD_EXTRA_SPACE}`
93
94 # Ensure total sectors is an integral number of sectors per
95 # track or mcopy will complain. Sectors are 512 bytes, and we
96 # generate images with 32 sectors per track. This calculation is
97 # done in blocks, thus the mod by 16 instead of 32.
98 BLOCKS=$(expr $BLOCKS + $(expr 16 - $(expr $BLOCKS % 16)))
99
100 mkdosfs -n ${BOOTDD_VOLUME_ID} -S 512 -C $HDDIMG $BLOCKS
101 mcopy -i $HDDIMG -s $HDDDIR/* ::/
102
103 if [ "${PCBIOS}" = "1" ]; then
104 syslinux_hdddirect_install $HDDIMG
105 fi
106 chmod 644 $HDDIMG
107
108 ROOTFSBLOCKS=`du -Lbks ${ROOTFS} | cut -f 1`
109 TOTALSIZE=`expr $BLOCKS + $ROOTFSBLOCKS`
110 END1=`expr $BLOCKS \* 1024`
111 END2=`expr $END1 + 512`
112 END3=`expr \( $ROOTFSBLOCKS \* 1024 \) + $END1`
113
114 echo $ROOTFSBLOCKS $TOTALSIZE $END1 $END2 $END3
115 rm -rf $IMAGE
116 dd if=/dev/zero of=$IMAGE bs=1024 seek=$TOTALSIZE count=1
117
118 parted $IMAGE mklabel msdos
119 parted $IMAGE mkpart primary fat16 0 ${END1}B
120 parted $IMAGE unit B mkpart primary ext2 ${END2}B ${END3}B
121 parted $IMAGE set 1 boot on
122 parted $IMAGE print
123
124 awk "BEGIN { printf \"$(echo ${DISK_SIGNATURE} | fold -w 2 | tac | paste -sd '' | sed 's/\(..\)/\\x&/g')\" }" | \
125 dd of=$IMAGE bs=1 seek=440 conv=notrunc
126
127 OFFSET=`expr $END2 / 512`
128 if [ "${PCBIOS}" = "1" ]; then
129 dd if=${STAGING_DATADIR}/syslinux/mbr.bin of=$IMAGE conv=notrunc
130 fi
131 dd if=$HDDIMG of=$IMAGE conv=notrunc seek=1 bs=512
132 dd if=${ROOTFS} of=$IMAGE conv=notrunc seek=$OFFSET bs=512
133
134 cd ${DEPLOY_DIR_IMAGE}
135 rm -f ${DEPLOY_DIR_IMAGE}/${IMAGE_LINK_NAME}.hdddirect
136 ln -s ${IMAGE_NAME}.hdddirect ${DEPLOY_DIR_IMAGE}/${IMAGE_LINK_NAME}.hdddirect
137}
138
139python do_bootdirectdisk() {
140 validate_disk_signature(d)
141 if d.getVar("PCBIOS", True) == "1":
142 bb.build.exec_func('build_syslinux_cfg', d)
143 if d.getVar("EFI", True) == "1":
144 bb.build.exec_func('build_grub_cfg', d)
145 bb.build.exec_func('build_boot_dd', d)
146}
147
148def generate_disk_signature():
149 import uuid
150
151 signature = str(uuid.uuid4())[:8]
152
153 if signature != '00000000':
154 return signature
155 else:
156 return 'ffffffff'
157
158def validate_disk_signature(d):
159 import re
160
161 disk_signature = d.getVar("DISK_SIGNATURE", True)
162
163 if not re.match(r'^[0-9a-fA-F]{8}$', disk_signature):
164 bb.fatal("DISK_SIGNATURE '%s' must be an 8 digit hex string" % disk_signature)
165
166DISK_SIGNATURE_GENERATED := "${@generate_disk_signature()}"
167
168addtask bootdirectdisk before do_build