blob: 600e21abcfee68836b76822492631c0b9eb638f2 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001# 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 virtual/kernel:do_deploy \
24 syslinux:do_populate_sysroot \
25 syslinux-native:do_populate_sysroot \
26 parted-native:do_populate_sysroot \
27 mtools-native:do_populate_sysroot "
28
29PACKAGES = " "
30EXCLUDE_FROM_WORLD = "1"
31
32BOOTDD_VOLUME_ID ?= "boot"
33BOOTDD_EXTRA_SPACE ?= "16384"
34
35EFI = "${@bb.utils.contains("MACHINE_FEATURES", "efi", "1", "0", d)}"
36EFI_PROVIDER ?= "grub-efi"
37EFI_CLASS = "${@bb.utils.contains("MACHINE_FEATURES", "efi", "${EFI_PROVIDER}", "", d)}"
38
39# Include legacy boot if MACHINE_FEATURES includes "pcbios" or if it does not
40# contain "efi". This way legacy is supported by default if neither is
41# specified, maintaining the original behavior.
42def pcbios(d):
43 pcbios = bb.utils.contains("MACHINE_FEATURES", "pcbios", "1", "0", d)
44 if pcbios == "0":
45 pcbios = bb.utils.contains("MACHINE_FEATURES", "efi", "0", "1", d)
46 return pcbios
47
48def pcbios_class(d):
49 if d.getVar("PCBIOS", True) == "1":
50 return "syslinux"
51 return ""
52
53PCBIOS = "${@pcbios(d)}"
54PCBIOS_CLASS = "${@pcbios_class(d)}"
55
56inherit ${PCBIOS_CLASS}
57inherit ${EFI_CLASS}
58
59# Get the build_syslinux_cfg() function from the syslinux class
60
61AUTO_SYSLINUXCFG = "1"
62DISK_SIGNATURE ?= "${DISK_SIGNATURE_GENERATED}"
63SYSLINUX_ROOT ?= "root=/dev/sda2"
64SYSLINUX_TIMEOUT ?= "10"
65
66IS_VM = '${@bb.utils.contains_any("IMAGE_FSTYPES", ["vmdk", "vdi", "qcow2"], "true", "false", d)}'
67
68boot_direct_populate() {
69 dest=$1
70 install -d $dest
71
72 # Install bzImage, initrd, and rootfs.img in DEST for all loaders to use.
73 if [ -e ${DEPLOY_DIR_IMAGE}/bzImage ]; then
74 install -m 0644 ${DEPLOY_DIR_IMAGE}/bzImage $dest/vmlinuz
75 fi
76
77 # initrd is made of concatenation of multiple filesystem images
78 if [ -n "${INITRD}" ]; then
79 rm -f $dest/initrd
80 for fs in ${INITRD}
81 do
82 if [ -s "${fs}" ]; then
83 cat ${fs} >> $dest/initrd
84 else
85 bbfatal "${fs} is invalid. initrd image creation failed."
86 fi
87 done
88 chmod 0644 $dest/initrd
89 fi
90}
91
92build_boot_dd() {
93 HDDDIR="${S}/hdd/boot"
94 HDDIMG="${S}/hdd.image"
95 IMAGE=${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}.hdddirect
96
97 boot_direct_populate $HDDDIR
98
99 if [ "${PCBIOS}" = "1" ]; then
100 syslinux_hddimg_populate $HDDDIR
101 fi
102 if [ "${EFI}" = "1" ]; then
103 efi_hddimg_populate $HDDDIR
104 fi
105
106 if [ "${IS_VM}" = "true" ]; then
107 if [ "x${AUTO_SYSLINUXMENU}" = "x1" ] ; then
108 install -m 0644 ${STAGING_DIR}/${MACHINE}/usr/share/syslinux/vesamenu.c32 $HDDDIR/${SYSLINUXDIR}/
109 if [ "x${SYSLINUX_SPLASH}" != "x" ] ; then
110 install -m 0644 ${SYSLINUX_SPLASH} $HDDDIR/${SYSLINUXDIR}/splash.lss
111 fi
112 fi
113 fi
114
115 BLOCKS=`du -bks $HDDDIR | cut -f 1`
116 BLOCKS=`expr $BLOCKS + ${BOOTDD_EXTRA_SPACE}`
117
118 # Ensure total sectors is an integral number of sectors per
119 # track or mcopy will complain. Sectors are 512 bytes, and we
120 # generate images with 32 sectors per track. This calculation is
121 # done in blocks, thus the mod by 16 instead of 32.
122 BLOCKS=$(expr $BLOCKS + $(expr 16 - $(expr $BLOCKS % 16)))
123
124 mkdosfs -n ${BOOTDD_VOLUME_ID} -S 512 -C $HDDIMG $BLOCKS
125 mcopy -i $HDDIMG -s $HDDDIR/* ::/
126
127 if [ "${PCBIOS}" = "1" ]; then
128 syslinux_hdddirect_install $HDDIMG
129 fi
130 chmod 644 $HDDIMG
131
132 ROOTFSBLOCKS=`du -Lbks ${ROOTFS} | cut -f 1`
133 TOTALSIZE=`expr $BLOCKS + $ROOTFSBLOCKS`
134 END1=`expr $BLOCKS \* 1024`
135 END2=`expr $END1 + 512`
136 END3=`expr \( $ROOTFSBLOCKS \* 1024 \) + $END1`
137
138 echo $ROOTFSBLOCKS $TOTALSIZE $END1 $END2 $END3
139 rm -rf $IMAGE
140 dd if=/dev/zero of=$IMAGE bs=1024 seek=$TOTALSIZE count=1
141
142 parted $IMAGE mklabel msdos
143 parted $IMAGE mkpart primary fat16 0 ${END1}B
144 parted $IMAGE unit B mkpart primary ext2 ${END2}B ${END3}B
145 parted $IMAGE set 1 boot on
146
147 parted $IMAGE print
148
149 awk "BEGIN { printf \"$(echo ${DISK_SIGNATURE} | fold -w 2 | tac | paste -sd '' | sed 's/\(..\)/\\x&/g')\" }" | \
150 dd of=$IMAGE bs=1 seek=440 conv=notrunc
151
152 OFFSET=`expr $END2 / 512`
153 if [ "${PCBIOS}" = "1" ]; then
154 dd if=${STAGING_DATADIR}/syslinux/mbr.bin of=$IMAGE conv=notrunc
155 fi
156
157 dd if=$HDDIMG of=$IMAGE conv=notrunc seek=1 bs=512
158 dd if=${ROOTFS} of=$IMAGE conv=notrunc seek=$OFFSET bs=512
159
160 cd ${DEPLOY_DIR_IMAGE}
161 rm -f ${DEPLOY_DIR_IMAGE}/${IMAGE_LINK_NAME}.hdddirect
162 ln -s ${IMAGE_NAME}.hdddirect ${DEPLOY_DIR_IMAGE}/${IMAGE_LINK_NAME}.hdddirect
163}
164
165python do_bootdirectdisk() {
166 validate_disk_signature(d)
167 if d.getVar("PCBIOS", True) == "1":
168 bb.build.exec_func('build_syslinux_cfg', d)
169 if d.getVar("EFI", True) == "1":
170 bb.build.exec_func('build_efi_cfg', d)
171 bb.build.exec_func('build_boot_dd', d)
172}
173
174def generate_disk_signature():
175 import uuid
176
177 signature = str(uuid.uuid4())[:8]
178
179 if signature != '00000000':
180 return signature
181 else:
182 return 'ffffffff'
183
184def validate_disk_signature(d):
185 import re
186
187 disk_signature = d.getVar("DISK_SIGNATURE", True)
188
189 if not re.match(r'^[0-9a-fA-F]{8}$', disk_signature):
190 bb.fatal("DISK_SIGNATURE '%s' must be an 8 digit hex string" % disk_signature)
191
192DISK_SIGNATURE_GENERATED := "${@generate_disk_signature()}"
193
194addtask bootdirectdisk before do_build