blob: 1623c15988b0f668ea8cf444873197d2d186f090 [file] [log] [blame]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001# Copyright (C) 2004, Advanced Micro Devices, Inc. All Rights Reserved
2# Released under the MIT license (see packages/COPYING)
Patrick Williamsc124f4f2015-09-15 14:41:29 -05003
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05004# Creates a bootable image using syslinux, your kernel and an optional
5# initrd
Patrick Williamsc124f4f2015-09-15 14:41:29 -05006
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05007#
8# End result is two things:
9#
10# 1. A .hddimg file which is an msdos filesystem containing syslinux, a kernel,
11# an initrd and a rootfs image. These can be written to harddisks directly and
12# also booted on USB flash disks (write them there with dd).
13#
14# 2. A CD .iso image
Patrick Williamsc124f4f2015-09-15 14:41:29 -050015
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050016# Boot process is that the initrd will boot and process which label was selected
17# in syslinux. Actions based on the label are then performed (e.g. installing to
18# an hdd)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050019
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050020# External variables (also used by syslinux.bbclass)
21# ${INITRD} - indicates a list of filesystem images to concatenate and use as an initrd (optional)
22# ${COMPRESSISO} - Transparent compress ISO, reduce size ~40% if set to 1
23# ${NOISO} - skip building the ISO image if set to 1
24# ${NOHDD} - skip building the HDD image if set to 1
25# ${HDDIMG_ID} - FAT image volume-id
26# ${ROOTFS} - indicates a filesystem image to include as the root filesystem (optional)
27
28inherit live-vm-common
29
30do_bootimg[depends] += "dosfstools-native:do_populate_sysroot \
31 mtools-native:do_populate_sysroot \
32 cdrtools-native:do_populate_sysroot \
33 virtual/kernel:do_deploy \
34 ${MLPREFIX}syslinux:do_populate_sysroot \
35 syslinux-native:do_populate_sysroot \
36 ${@oe.utils.ifelse(d.getVar('COMPRESSISO', False),'zisofs-tools-native:do_populate_sysroot','')} \
Brad Bishopd7bf8c12018-02-25 22:55:05 -050037 ${PN}:do_image_${@d.getVar('LIVE_ROOTFS_TYPE').replace('-', '_')} \
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050038 "
39
40
41LABELS_LIVE ?= "boot install"
42ROOT_LIVE ?= "root=/dev/ram0"
Brad Bishopd7bf8c12018-02-25 22:55:05 -050043INITRD_IMAGE_LIVE ?= "${MLPREFIX}core-image-minimal-initramfs"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050044INITRD_LIVE ?= "${DEPLOY_DIR_IMAGE}/${INITRD_IMAGE_LIVE}-${MACHINE}.cpio.gz"
45
Brad Bishopd7bf8c12018-02-25 22:55:05 -050046LIVE_ROOTFS_TYPE ?= "ext4"
47ROOTFS ?= "${IMGDEPLOYDIR}/${IMAGE_LINK_NAME}.${LIVE_ROOTFS_TYPE}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050048
Brad Bishopd7bf8c12018-02-25 22:55:05 -050049IMAGE_TYPEDEP_live = "${LIVE_ROOTFS_TYPE}"
50IMAGE_TYPEDEP_iso = "${LIVE_ROOTFS_TYPE}"
51IMAGE_TYPEDEP_hddimg = "${LIVE_ROOTFS_TYPE}"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050052IMAGE_TYPES_MASKED += "live hddimg iso"
53
54python() {
Brad Bishop6e60e8b2018-02-01 10:27:11 -050055 image_b = d.getVar('IMAGE_BASENAME')
56 initrd_i = d.getVar('INITRD_IMAGE_LIVE')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050057 if image_b == initrd_i:
58 bb.error('INITRD_IMAGE_LIVE %s cannot use image live, hddimg or iso.' % initrd_i)
59 bb.fatal('Check IMAGE_FSTYPES and INITRAMFS_FSTYPES settings.')
Patrick Williamsc0f7c042017-02-23 20:41:17 -060060 elif initrd_i:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050061 d.appendVarFlag('do_bootimg', 'depends', ' %s:do_image_complete' % initrd_i)
62}
63
64HDDDIR = "${S}/hddimg"
65ISODIR = "${S}/iso"
66EFIIMGDIR = "${S}/efi_img"
67COMPACT_ISODIR = "${S}/iso.z"
68COMPRESSISO ?= "0"
69
70ISOLINUXDIR ?= "/isolinux"
71ISO_BOOTIMG = "isolinux/isolinux.bin"
72ISO_BOOTCAT = "isolinux/boot.cat"
73MKISOFS_OPTIONS = "-no-emul-boot -boot-load-size 4 -boot-info-table"
74
75BOOTIMG_VOLUME_ID ?= "boot"
76BOOTIMG_EXTRA_SPACE ?= "512"
77
78populate_live() {
79 populate_kernel $1
80 if [ -s "${ROOTFS}" ]; then
81 install -m 0644 ${ROOTFS} $1/rootfs.img
82 fi
83}
84
85build_iso() {
86 # Only create an ISO if we have an INITRD and NOISO was not set
87 if [ -z "${INITRD}" ] || [ "${NOISO}" = "1" ]; then
88 bbnote "ISO image will not be created."
89 return
90 fi
91 # ${INITRD} is a list of multiple filesystem images
92 for fs in ${INITRD}
93 do
94 if [ ! -s "$fs" ]; then
Brad Bishopd7bf8c12018-02-25 22:55:05 -050095 bbwarn "ISO image will not be created. $fs is invalid."
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050096 return
97 fi
98 done
99
100 populate_live ${ISODIR}
101
102 if [ "${PCBIOS}" = "1" ]; then
103 syslinux_iso_populate ${ISODIR}
104 fi
105 if [ "${EFI}" = "1" ]; then
106 efi_iso_populate ${ISODIR}
107 build_fat_img ${EFIIMGDIR} ${ISODIR}/efi.img
108 fi
109
110 # EFI only
111 if [ "${PCBIOS}" != "1" ] && [ "${EFI}" = "1" ] ; then
112 # Work around bug in isohybrid where it requires isolinux.bin
113 # In the boot catalog, even though it is not used
114 mkdir -p ${ISODIR}/${ISOLINUXDIR}
115 install -m 0644 ${STAGING_DATADIR}/syslinux/isolinux.bin ${ISODIR}${ISOLINUXDIR}
116 fi
117
118 if [ "${COMPRESSISO}" = "1" ] ; then
119 # create compact directory, compress iso
120 mkdir -p ${COMPACT_ISODIR}
121 mkzftree -z 9 -p 4 -F ${ISODIR}/rootfs.img ${COMPACT_ISODIR}/rootfs.img
122
123 # move compact iso to iso, then remove compact directory
124 mv ${COMPACT_ISODIR}/rootfs.img ${ISODIR}/rootfs.img
125 rm -Rf ${COMPACT_ISODIR}
126 mkisofs_compress_opts="-R -z -D -l"
127 else
128 mkisofs_compress_opts="-r"
129 fi
130
131 # Check the size of ${ISODIR}/rootfs.img, use mkisofs -iso-level 3
132 # when it exceeds 3.8GB, the specification is 4G - 1 bytes, we need
133 # leave a few space for other files.
134 mkisofs_iso_level=""
135
136 if [ -n "${ROOTFS}" ] && [ -s "${ROOTFS}" ]; then
137 rootfs_img_size=`stat -c '%s' ${ISODIR}/rootfs.img`
138 # 4080218931 = 3.8 * 1024 * 1024 * 1024
139 if [ $rootfs_img_size -gt 4080218931 ]; then
140 bbnote "${ISODIR}/rootfs.img execeeds 3.8GB, using '-iso-level 3' for mkisofs"
141 mkisofs_iso_level="-iso-level 3"
142 fi
143 fi
144
145 if [ "${PCBIOS}" = "1" ] && [ "${EFI}" != "1" ] ; then
146 # PCBIOS only media
147 mkisofs -V ${BOOTIMG_VOLUME_ID} \
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600148 -o ${IMGDEPLOYDIR}/${IMAGE_NAME}.iso \
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500149 -b ${ISO_BOOTIMG} -c ${ISO_BOOTCAT} \
150 $mkisofs_compress_opts \
151 ${MKISOFS_OPTIONS} $mkisofs_iso_level ${ISODIR}
152 else
153 # EFI only OR EFI+PCBIOS
154 mkisofs -A ${BOOTIMG_VOLUME_ID} -V ${BOOTIMG_VOLUME_ID} \
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600155 -o ${IMGDEPLOYDIR}/${IMAGE_NAME}.iso \
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500156 -b ${ISO_BOOTIMG} -c ${ISO_BOOTCAT} \
157 $mkisofs_compress_opts ${MKISOFS_OPTIONS} $mkisofs_iso_level \
158 -eltorito-alt-boot -eltorito-platform efi \
159 -b efi.img -no-emul-boot \
160 ${ISODIR}
161 isohybrid_args="-u"
162 fi
163
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600164 isohybrid $isohybrid_args ${IMGDEPLOYDIR}/${IMAGE_NAME}.iso
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500165}
166
167build_fat_img() {
168 FATSOURCEDIR=$1
169 FATIMG=$2
170
171 # Calculate the size required for the final image including the
172 # data and filesystem overhead.
173 # Sectors: 512 bytes
174 # Blocks: 1024 bytes
175
176 # Determine the sector count just for the data
177 SECTORS=$(expr $(du --apparent-size -ks ${FATSOURCEDIR} | cut -f 1) \* 2)
178
179 # Account for the filesystem overhead. This includes directory
180 # entries in the clusters as well as the FAT itself.
181 # Assumptions:
182 # FAT32 (12 or 16 may be selected by mkdosfs, but the extra
183 # padding will be minimal on those smaller images and not
184 # worth the logic here to caclulate the smaller FAT sizes)
185 # < 16 entries per directory
186 # 8.3 filenames only
187
188 # 32 bytes per dir entry
189 DIR_BYTES=$(expr $(find ${FATSOURCEDIR} | tail -n +2 | wc -l) \* 32)
190 # 32 bytes for every end-of-directory dir entry
191 DIR_BYTES=$(expr $DIR_BYTES + $(expr $(find ${FATSOURCEDIR} -type d | tail -n +2 | wc -l) \* 32))
192 # 4 bytes per FAT entry per sector of data
193 FAT_BYTES=$(expr $SECTORS \* 4)
194 # 4 bytes per FAT entry per end-of-cluster list
195 FAT_BYTES=$(expr $FAT_BYTES + $(expr $(find ${FATSOURCEDIR} -type d | tail -n +2 | wc -l) \* 4))
196
197 # Use a ceiling function to determine FS overhead in sectors
198 DIR_SECTORS=$(expr $(expr $DIR_BYTES + 511) / 512)
199 # There are two FATs on the image
200 FAT_SECTORS=$(expr $(expr $(expr $FAT_BYTES + 511) / 512) \* 2)
201 SECTORS=$(expr $SECTORS + $(expr $DIR_SECTORS + $FAT_SECTORS))
202
203 # Determine the final size in blocks accounting for some padding
204 BLOCKS=$(expr $(expr $SECTORS / 2) + ${BOOTIMG_EXTRA_SPACE})
205
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500206 # mkdosfs will sometimes use FAT16 when it is not appropriate,
207 # resulting in a boot failure from SYSLINUX. Use FAT32 for
208 # images larger than 512MB, otherwise let mkdosfs decide.
209 if [ $(expr $BLOCKS / 1024) -gt 512 ]; then
210 FATSIZE="-F 32"
211 fi
212
213 # mkdosfs will fail if ${FATIMG} exists. Since we are creating an
214 # new image, it is safe to delete any previous image.
215 if [ -e ${FATIMG} ]; then
216 rm ${FATIMG}
217 fi
218
219 if [ -z "${HDDIMG_ID}" ]; then
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500220 mkdosfs ${FATSIZE} -n ${BOOTIMG_VOLUME_ID} ${MKDOSFS_EXTRAOPTS} -C ${FATIMG} \
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500221 ${BLOCKS}
222 else
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500223 mkdosfs ${FATSIZE} -n ${BOOTIMG_VOLUME_ID} ${MKDOSFS_EXTRAOPTS} -C ${FATIMG} \
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500224 ${BLOCKS} -i ${HDDIMG_ID}
225 fi
226
227 # Copy FATSOURCEDIR recursively into the image file directly
228 mcopy -i ${FATIMG} -s ${FATSOURCEDIR}/* ::/
229}
230
231build_hddimg() {
232 # Create an HDD image
233 if [ "${NOHDD}" != "1" ] ; then
234 populate_live ${HDDDIR}
235
236 if [ "${PCBIOS}" = "1" ]; then
237 syslinux_hddimg_populate ${HDDDIR}
238 fi
239 if [ "${EFI}" = "1" ]; then
240 efi_hddimg_populate ${HDDDIR}
241 fi
242
243 # Check the size of ${HDDDIR}/rootfs.img, error out if it
244 # exceeds 4GB, it is the single file's max size of FAT fs.
245 if [ -f ${HDDDIR}/rootfs.img ]; then
246 rootfs_img_size=`stat -c '%s' ${HDDDIR}/rootfs.img`
247 max_size=`expr 4 \* 1024 \* 1024 \* 1024`
248 if [ $rootfs_img_size -gt $max_size ]; then
249 bberror "${HDDDIR}/rootfs.img execeeds 4GB,"
250 bberror "this doesn't work on FAT filesystem, you can try either of:"
251 bberror "1) Reduce the size of rootfs.img"
252 bbfatal "2) Use iso, vmdk or vdi to instead of hddimg\n"
253 fi
254 fi
255
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600256 build_fat_img ${HDDDIR} ${IMGDEPLOYDIR}/${IMAGE_NAME}.hddimg
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500257
258 if [ "${PCBIOS}" = "1" ]; then
259 syslinux_hddimg_install
260 fi
261
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600262 chmod 644 ${IMGDEPLOYDIR}/${IMAGE_NAME}.hddimg
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500263 fi
264}
265
266python do_bootimg() {
267 set_live_vm_vars(d, 'LIVE')
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500268 if d.getVar("PCBIOS") == "1":
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500269 bb.build.exec_func('build_syslinux_cfg', d)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500270 if d.getVar("EFI") == "1":
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500271 bb.build.exec_func('build_efi_cfg', d)
272 bb.build.exec_func('build_hddimg', d)
273 bb.build.exec_func('build_iso', d)
274 bb.build.exec_func('create_symlinks', d)
275}
276do_bootimg[subimages] = "hddimg iso"
277do_bootimg[imgsuffix] = "."
278
279addtask bootimg before do_image_complete