blob: 966277c67853ef06cacd38ebeab889d0a18d4a30 [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)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050022# ${NOISO} - skip building the ISO image if set to 1
23# ${NOHDD} - skip building the HDD image if set to 1
24# ${HDDIMG_ID} - FAT image volume-id
25# ${ROOTFS} - indicates a filesystem image to include as the root filesystem (optional)
26
27inherit live-vm-common
28
29do_bootimg[depends] += "dosfstools-native:do_populate_sysroot \
30 mtools-native:do_populate_sysroot \
31 cdrtools-native:do_populate_sysroot \
32 virtual/kernel:do_deploy \
33 ${MLPREFIX}syslinux:do_populate_sysroot \
34 syslinux-native:do_populate_sysroot \
Brad Bishopd7bf8c12018-02-25 22:55:05 -050035 ${PN}:do_image_${@d.getVar('LIVE_ROOTFS_TYPE').replace('-', '_')} \
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050036 "
37
38
39LABELS_LIVE ?= "boot install"
40ROOT_LIVE ?= "root=/dev/ram0"
Brad Bishopd7bf8c12018-02-25 22:55:05 -050041INITRD_IMAGE_LIVE ?= "${MLPREFIX}core-image-minimal-initramfs"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050042INITRD_LIVE ?= "${DEPLOY_DIR_IMAGE}/${INITRD_IMAGE_LIVE}-${MACHINE}.cpio.gz"
43
Brad Bishopd7bf8c12018-02-25 22:55:05 -050044LIVE_ROOTFS_TYPE ?= "ext4"
45ROOTFS ?= "${IMGDEPLOYDIR}/${IMAGE_LINK_NAME}.${LIVE_ROOTFS_TYPE}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050046
Brad Bishopd7bf8c12018-02-25 22:55:05 -050047IMAGE_TYPEDEP_live = "${LIVE_ROOTFS_TYPE}"
48IMAGE_TYPEDEP_iso = "${LIVE_ROOTFS_TYPE}"
49IMAGE_TYPEDEP_hddimg = "${LIVE_ROOTFS_TYPE}"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050050IMAGE_TYPES_MASKED += "live hddimg iso"
51
52python() {
Brad Bishop6e60e8b2018-02-01 10:27:11 -050053 image_b = d.getVar('IMAGE_BASENAME')
54 initrd_i = d.getVar('INITRD_IMAGE_LIVE')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050055 if image_b == initrd_i:
56 bb.error('INITRD_IMAGE_LIVE %s cannot use image live, hddimg or iso.' % initrd_i)
57 bb.fatal('Check IMAGE_FSTYPES and INITRAMFS_FSTYPES settings.')
Patrick Williamsc0f7c042017-02-23 20:41:17 -060058 elif initrd_i:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050059 d.appendVarFlag('do_bootimg', 'depends', ' %s:do_image_complete' % initrd_i)
60}
61
62HDDDIR = "${S}/hddimg"
63ISODIR = "${S}/iso"
64EFIIMGDIR = "${S}/efi_img"
65COMPACT_ISODIR = "${S}/iso.z"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050066
67ISOLINUXDIR ?= "/isolinux"
68ISO_BOOTIMG = "isolinux/isolinux.bin"
69ISO_BOOTCAT = "isolinux/boot.cat"
70MKISOFS_OPTIONS = "-no-emul-boot -boot-load-size 4 -boot-info-table"
71
72BOOTIMG_VOLUME_ID ?= "boot"
73BOOTIMG_EXTRA_SPACE ?= "512"
74
75populate_live() {
76 populate_kernel $1
77 if [ -s "${ROOTFS}" ]; then
78 install -m 0644 ${ROOTFS} $1/rootfs.img
79 fi
80}
81
82build_iso() {
83 # Only create an ISO if we have an INITRD and NOISO was not set
84 if [ -z "${INITRD}" ] || [ "${NOISO}" = "1" ]; then
85 bbnote "ISO image will not be created."
86 return
87 fi
88 # ${INITRD} is a list of multiple filesystem images
89 for fs in ${INITRD}
90 do
91 if [ ! -s "$fs" ]; then
Brad Bishopd7bf8c12018-02-25 22:55:05 -050092 bbwarn "ISO image will not be created. $fs is invalid."
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050093 return
94 fi
95 done
96
97 populate_live ${ISODIR}
98
99 if [ "${PCBIOS}" = "1" ]; then
100 syslinux_iso_populate ${ISODIR}
101 fi
102 if [ "${EFI}" = "1" ]; then
103 efi_iso_populate ${ISODIR}
104 build_fat_img ${EFIIMGDIR} ${ISODIR}/efi.img
105 fi
106
107 # EFI only
108 if [ "${PCBIOS}" != "1" ] && [ "${EFI}" = "1" ] ; then
109 # Work around bug in isohybrid where it requires isolinux.bin
110 # In the boot catalog, even though it is not used
111 mkdir -p ${ISODIR}/${ISOLINUXDIR}
112 install -m 0644 ${STAGING_DATADIR}/syslinux/isolinux.bin ${ISODIR}${ISOLINUXDIR}
113 fi
114
Brad Bishop316dfdd2018-06-25 12:45:53 -0400115 # We used to have support for zisofs; this is a relic of that
116 mkisofs_compress_opts="-r"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500117
118 # Check the size of ${ISODIR}/rootfs.img, use mkisofs -iso-level 3
119 # when it exceeds 3.8GB, the specification is 4G - 1 bytes, we need
120 # leave a few space for other files.
121 mkisofs_iso_level=""
122
123 if [ -n "${ROOTFS}" ] && [ -s "${ROOTFS}" ]; then
124 rootfs_img_size=`stat -c '%s' ${ISODIR}/rootfs.img`
125 # 4080218931 = 3.8 * 1024 * 1024 * 1024
126 if [ $rootfs_img_size -gt 4080218931 ]; then
127 bbnote "${ISODIR}/rootfs.img execeeds 3.8GB, using '-iso-level 3' for mkisofs"
128 mkisofs_iso_level="-iso-level 3"
129 fi
130 fi
131
132 if [ "${PCBIOS}" = "1" ] && [ "${EFI}" != "1" ] ; then
133 # PCBIOS only media
134 mkisofs -V ${BOOTIMG_VOLUME_ID} \
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600135 -o ${IMGDEPLOYDIR}/${IMAGE_NAME}.iso \
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500136 -b ${ISO_BOOTIMG} -c ${ISO_BOOTCAT} \
137 $mkisofs_compress_opts \
138 ${MKISOFS_OPTIONS} $mkisofs_iso_level ${ISODIR}
139 else
140 # EFI only OR EFI+PCBIOS
141 mkisofs -A ${BOOTIMG_VOLUME_ID} -V ${BOOTIMG_VOLUME_ID} \
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600142 -o ${IMGDEPLOYDIR}/${IMAGE_NAME}.iso \
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500143 -b ${ISO_BOOTIMG} -c ${ISO_BOOTCAT} \
144 $mkisofs_compress_opts ${MKISOFS_OPTIONS} $mkisofs_iso_level \
145 -eltorito-alt-boot -eltorito-platform efi \
146 -b efi.img -no-emul-boot \
147 ${ISODIR}
148 isohybrid_args="-u"
149 fi
150
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600151 isohybrid $isohybrid_args ${IMGDEPLOYDIR}/${IMAGE_NAME}.iso
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500152}
153
154build_fat_img() {
155 FATSOURCEDIR=$1
156 FATIMG=$2
157
158 # Calculate the size required for the final image including the
159 # data and filesystem overhead.
160 # Sectors: 512 bytes
161 # Blocks: 1024 bytes
162
163 # Determine the sector count just for the data
164 SECTORS=$(expr $(du --apparent-size -ks ${FATSOURCEDIR} | cut -f 1) \* 2)
165
166 # Account for the filesystem overhead. This includes directory
167 # entries in the clusters as well as the FAT itself.
168 # Assumptions:
169 # FAT32 (12 or 16 may be selected by mkdosfs, but the extra
170 # padding will be minimal on those smaller images and not
171 # worth the logic here to caclulate the smaller FAT sizes)
172 # < 16 entries per directory
173 # 8.3 filenames only
174
175 # 32 bytes per dir entry
176 DIR_BYTES=$(expr $(find ${FATSOURCEDIR} | tail -n +2 | wc -l) \* 32)
177 # 32 bytes for every end-of-directory dir entry
178 DIR_BYTES=$(expr $DIR_BYTES + $(expr $(find ${FATSOURCEDIR} -type d | tail -n +2 | wc -l) \* 32))
179 # 4 bytes per FAT entry per sector of data
180 FAT_BYTES=$(expr $SECTORS \* 4)
181 # 4 bytes per FAT entry per end-of-cluster list
182 FAT_BYTES=$(expr $FAT_BYTES + $(expr $(find ${FATSOURCEDIR} -type d | tail -n +2 | wc -l) \* 4))
183
184 # Use a ceiling function to determine FS overhead in sectors
185 DIR_SECTORS=$(expr $(expr $DIR_BYTES + 511) / 512)
186 # There are two FATs on the image
187 FAT_SECTORS=$(expr $(expr $(expr $FAT_BYTES + 511) / 512) \* 2)
188 SECTORS=$(expr $SECTORS + $(expr $DIR_SECTORS + $FAT_SECTORS))
189
190 # Determine the final size in blocks accounting for some padding
191 BLOCKS=$(expr $(expr $SECTORS / 2) + ${BOOTIMG_EXTRA_SPACE})
192
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500193 # mkdosfs will sometimes use FAT16 when it is not appropriate,
194 # resulting in a boot failure from SYSLINUX. Use FAT32 for
195 # images larger than 512MB, otherwise let mkdosfs decide.
196 if [ $(expr $BLOCKS / 1024) -gt 512 ]; then
197 FATSIZE="-F 32"
198 fi
199
200 # mkdosfs will fail if ${FATIMG} exists. Since we are creating an
201 # new image, it is safe to delete any previous image.
202 if [ -e ${FATIMG} ]; then
203 rm ${FATIMG}
204 fi
205
206 if [ -z "${HDDIMG_ID}" ]; then
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500207 mkdosfs ${FATSIZE} -n ${BOOTIMG_VOLUME_ID} ${MKDOSFS_EXTRAOPTS} -C ${FATIMG} \
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500208 ${BLOCKS}
209 else
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500210 mkdosfs ${FATSIZE} -n ${BOOTIMG_VOLUME_ID} ${MKDOSFS_EXTRAOPTS} -C ${FATIMG} \
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500211 ${BLOCKS} -i ${HDDIMG_ID}
212 fi
213
214 # Copy FATSOURCEDIR recursively into the image file directly
215 mcopy -i ${FATIMG} -s ${FATSOURCEDIR}/* ::/
216}
217
218build_hddimg() {
219 # Create an HDD image
220 if [ "${NOHDD}" != "1" ] ; then
221 populate_live ${HDDDIR}
222
223 if [ "${PCBIOS}" = "1" ]; then
224 syslinux_hddimg_populate ${HDDDIR}
225 fi
226 if [ "${EFI}" = "1" ]; then
227 efi_hddimg_populate ${HDDDIR}
228 fi
229
230 # Check the size of ${HDDDIR}/rootfs.img, error out if it
231 # exceeds 4GB, it is the single file's max size of FAT fs.
232 if [ -f ${HDDDIR}/rootfs.img ]; then
233 rootfs_img_size=`stat -c '%s' ${HDDDIR}/rootfs.img`
234 max_size=`expr 4 \* 1024 \* 1024 \* 1024`
235 if [ $rootfs_img_size -gt $max_size ]; then
236 bberror "${HDDDIR}/rootfs.img execeeds 4GB,"
237 bberror "this doesn't work on FAT filesystem, you can try either of:"
238 bberror "1) Reduce the size of rootfs.img"
239 bbfatal "2) Use iso, vmdk or vdi to instead of hddimg\n"
240 fi
241 fi
242
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600243 build_fat_img ${HDDDIR} ${IMGDEPLOYDIR}/${IMAGE_NAME}.hddimg
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500244
245 if [ "${PCBIOS}" = "1" ]; then
246 syslinux_hddimg_install
247 fi
248
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600249 chmod 644 ${IMGDEPLOYDIR}/${IMAGE_NAME}.hddimg
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500250 fi
251}
252
253python do_bootimg() {
254 set_live_vm_vars(d, 'LIVE')
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500255 if d.getVar("PCBIOS") == "1":
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500256 bb.build.exec_func('build_syslinux_cfg', d)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500257 if d.getVar("EFI") == "1":
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500258 bb.build.exec_func('build_efi_cfg', d)
259 bb.build.exec_func('build_hddimg', d)
260 bb.build.exec_func('build_iso', d)
261 bb.build.exec_func('create_symlinks', d)
262}
263do_bootimg[subimages] = "hddimg iso"
264do_bootimg[imgsuffix] = "."
265
266addtask bootimg before do_image_complete