Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
#	$NetBSD: Makefile.bootimage,v 1.21.2.2 2020/04/24 17:19:49 martin Exp $
#
# Copyright (c) 2009, 2010, 2011 Izumi Tsutsui.  All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#
# Makefile to create a bootable FS image for USB flash or emulators
#

#
# Required variables:
#	RELEASEDIR
#		Should be defined in nbmake-${MACHINE}
#	IMGBASE
#		Basename of the image
#
# Optional variables:
#	USE_MBR
#		set yes if target disk image requires MBR partition
#		(default: no)
#	USE_GPT
#		set yes if target disk image requires GPT partition
#		(default: no)
#	MBR_BOOTCODE
#		optional MBR bootcode which should be installed by fdisk(8)
#		(default: empty)
#		- specified MBR_BOOTCODE must exist in ${DESTDIR}/usr/mdec
#		- if MBR_BOOTCODE is not specified,
#		  MBR_DEFAULT_BOOTCODE (default: mbr) will be used
#		  if the target ${MACHINE} has the one in /usr/mdec
#	USE_SUNLABEL
#		set yes if target disk image requires Sun's label
#		(default: no)
#	INSTALLBOOT_AFTER_DISKLABEL (untested)
#		set yes if the target ${MACHINE} requires disklabel
#		to run installboot(8), like hp300
#		(default: empty)
#	IMAGEMB
#		target image size in MB
#		(default: 2048)
#	SWAPMB
#		swap size in target image in MB
#		(default: 128)
#	EFIMB
#		EFI partition size in target image in MB
#		(default: 128)
#	FATMB
#		FAT partition size in target image in MB
#		(default: 0)
#	KERN_SET
#		kernel set name which should be extracted into image
#		(default: kern-GENERIC)
#	SETS
#		binary sets that should be extracted into image
#		(default: modules base etc comp games man misc rescue tests
#			  text xbase xcomp xetc xfont xserver)
#	SETS_DIR
#		directory path where binary sets are stored
#		(default: ${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/sets)
#	IMGFILE_EXTRA
#		list of additional files to be copied into images,
#		containing one or more tuples of the form:
#			FILE	TARGETPATH
#		for installation image etc.
#		(default: empty)
#	IMGDIR_EXTRA
#		list of additional directories to be copied into images,
#		containing one or more tuples of the form:
#			DIR	TARGETPATH
#		for installation image etc.
#		(default: empty)
#		XXX: currently permissions in IMGDIR_EXTRA are not handled
#	IMGDIR_EXCLUDE
#		pax(1) options to exclude files which should not copied
#		into TARGETPATH in IMGDIR_EXTRA
#		(default: empty)
#	FSTAB_IN
#		template file of /etc/fstab
#		(default: ${DISTRIBDIR}/common/bootimage/fstab.in)
#	SPEC_IN
#		default files of spec file for makefs(8)
#		(default: ${DISTRIBDIR}/common/bootimage/spec.in)
#	SPEC_EXTRA
#		additional files of spec file for makefs(8)
#		(default: empty)
#	IMGMAKEFSOPTIONS
#		options passed to makefs(8) to create root file system
#		(default: -o bsize=16384,fsize=2048,density=8192)
#	INSTALLBOOTOPTIONS
#		options passed to installboot(8), e.g., -o console=com0
#		(default: empty)
#	PRIMARY_BOOT
#		primary boot loader that should be installed into
#		the target image via installboot(8)
#		(default: empty)
#	SECONDARY_BOOT
#		secondary bootloader that should be put into the target image
#		(default: empty)
#	SECONDARY_BOOT_ARG
#		extra arguments that should be passed to installboot(8)
#		to specify the secondary bootloader
#		(default: empty)
#	DISKPROTO_IN
#		template file of disklabel -R
#		(default: ${DISTRIBDIR}/common/bootimage/diskproto.in
#		       or ${DISTRIBDIR}/common/bootimage/diskproto.mbr.in)
#	EFIBOOT
#		EFI bootloader
#		(default: empty)
#	MBRFAT
#		MBR partition type value for FAT
#		(default: 6 ("16-bit FAT, more than 32M"))
#	FATMAKEFSOPTIONS
#		options passed to makefs(8) to create a FAT partition
#		(default: -o fat_type=16)
#	FATFILES
#		list of additional files to be copied into FAT partition
#		(default: empty)
#

.include <bsd.own.mk> 		#
.include <bsd.endian.mk>	# for TARGET_ENDIANNESS

.include "${NETBSDSRCDIR}/distrib/common/Makefile.distrib"

.if ${MKREPRO_TIMESTAMP:Uno} != "no"
MAKEFS_TIMESTAMP=-T "${MKREPRO_TIMESTAMP}"
GPT_TIMESTAMP=-T "${MKREPRO_TIMESTAMP}"
PAX_TIMESTAMP=--timestamp "${MKREPRO_TIMESTAMP}"
.endif

.if empty(IMGBASE)
.BEGIN:
	@echo "Error: IMGBASE is not set"
	@false
.endif

# should be defined elsewhere? 
CAT?=		cat
CHMOD?=		chmod
DD?=		dd
MKDIR?=		mkdir -p
RM?=		rm

#
# common definitions for image
#
USE_MBR?=	no
USE_GPT?=	no
USE_SUNLABEL?=	no
INSTALLBOOT_AFTER_DISKLABEL?=	no

#
# size parameters for image
#
IMAGEMB?=	2048		# 2048MB
SWAPMB?=	128		# 128MB
.if ${USE_GPT} == "no"
EFIMB?=		0
GPTSECTORS=	0
.else
EFIMB?=		128		# 128MB
GPTSECTORS=	2048
.endif
FATMB?=		0

# XXX: SWAPMB could be zero and expr(1) returns exit status 1 in that case
IMAGESECTORS!=	expr ${IMAGEMB} \* 1024 \* 1024 / 512
SWAPSECTORS!=	expr ${SWAPMB} \* 1024 \* 1024 / 512 || true
EFISECTORS!=	expr ${EFIMB} \* 1024 \* 1024 / 512 || true
FATSECTORS!=	expr ${FATMB} \* 1024 \* 1024 / 512 || true

.if ${USE_MBR} == "no" && ${USE_GPT} == "no"
LABELSECTORS?=	0
.else
#LABELSECTORS?=	63	# historical
#LABELSECTORS?=	32	# 16KB aligned
LABELSECTORS?=	2048	# 1MB aligned for modern flash devices
.endif

FSSECTORS!=	expr ${IMAGESECTORS} - ${SWAPSECTORS} - ${LABELSECTORS} \
		    - ${EFISECTORS} - ${GPTSECTORS} - ${FATSECTORS}
FSSIZE!=	expr ${FSSECTORS} \* 512

# parameters for disklabel and MBR
HEADS=		64
SECTORS=	32
CYLINDERS!=	expr ${IMAGESECTORS} / \( ${HEADS} \* ${SECTORS} \)
SECPERCYLINDERS!= expr ${HEADS} \* ${SECTORS}
MBRHEADS=	255
MBRSECTORS=	63
MBRCYLINDERS!=	expr ${IMAGESECTORS} / \( ${MBRHEADS} \* ${MBRSECTORS} \)
MBRNETBSD=	169
MBRFAT?=	6	# 16-bit FAT, more than 32M

BSDPARTSECTORS!= expr ${IMAGESECTORS} - ${LABELSECTORS} \
		    - ${EFISECTORS} - ${GPTSECTORS} - ${FATSECTORS}
FSOFFSET!=	expr ${LABELSECTORS} + ${EFISECTORS} + ${FATSECTORS}
SWAPOFFSET!=	expr ${LABELSECTORS} + ${FSSECTORS} \
		    + ${EFISECTORS} + ${FATSECTORS}
FATOFFSET=	${LABELSECTORS}

# parameters for sunlabel
FSCYLINDERS!=	expr ${FSSECTORS} / \( ${HEADS} \* ${SECTORS} \)
SWAPCYLINDERS!=	expr ${SWAPSECTORS} / \( ${HEADS} \* ${SECTORS} \) || true

.if ${USE_GPT} != "no"
BOOTDISK_UUID=`${TOOL_GPT} ${GPT_TIMESSTAMP} ${WORKMBR} show -i 2 | ${TOOL_AWK} '/^GUID/ {print $$2}'`
SWAPDISK_UUID=`${TOOL_GPT} ${GPT_TIMESSTAMP} ${WORKMBR} show -i 3 | ${TOOL_AWK} '/^GUID/ {print $$2}'`
.endif

#
# definitions to create root fs
#
SETS_DEFAULT=	modules base etc comp games man misc rescue tests text
.if ${MKX11} != "no"
SETS_DEFAULT+=	xbase xcomp xetc xfont xserver
.endif

KERN_SET?=	kern-GENERIC
SETS?=		${SETS_DEFAULT}
IMG_SETS=	${KERN_SET} ${SETS}
SETS_DIR?=	${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/sets

FSTAB_IN?=	${DISTRIBDIR}/common/bootimage/fstab.in
SPEC_IN?=	${DISTRIBDIR}/common/bootimage/spec.in

IMGMAKEFSOPTIONS?=	-o bsize=16384,fsize=2048,density=8192
FATMAKEFSOPTIONS?=	-o fat_type=16

WORKDIR?=       work
WORKSPEC?=      work.spec
WORKFSTAB?=	work.fstab
WORKRCCONF?=	work.rc.conf
WORKFS?=        work.rootfs
TARGETFS?=      imgroot.fs
COMPRESS_PROGRAM=${"${USE_XZ_SETS:Uno}"!="no":?${TOOL_XZ}:${TOOL_GZIP}}
TAR_SUFF=	${"${USE_XZ_SETS:Uno}"!="no":?tar.xz:tgz}

CLEANFILES+=	${WORKSPEC} ${WORKFSTAB} ${WORKRCCONF} ${WORKFS}
CLEANFILES+=	${TARGETFS}

#
# definitions to create image
#
.if ${SWAPSECTORS} == 0
.  if ${USE_MBR} != "no"
DISKPROTO_IN?=	${DISTRIBDIR}/common/bootimage/diskproto.noswap.mbr.in
.  else
DISKPROTO_IN?=	${DISTRIBDIR}/common/bootimage/diskproto.noswap.in
.  endif
.else
.  if ${USE_MBR} != "no"
DISKPROTO_IN?=	${DISTRIBDIR}/common/bootimage/diskproto.mbr.in
.  else
DISKPROTO_IN?=	${DISTRIBDIR}/common/bootimage/diskproto.in
.  endif
.endif

MBR_DEFAULT_BOOTCODE?=	mbr

WORKMBR?=	work.mbr
WORKMBRTRUNC?=	work.mbr.truncated
WORKSWAP?=	work.swap
WORKEFI?=	work.efi
WORKGPT?=	work.gpt
WORKLABEL?=	work.diskproto
WORKIMG?=	work.img
EFIWORKDIR?=	work.efidir
WORKFAT?=	work.fat
WORKFATDIR?=	work.fatdir

.if ${USE_GPT} != "no"
${WORKFSTAB}: ${WORKMBR}
.endif

${WORKFSTAB}:
	@echo Preparing /etc/fstab ...
.if ${USE_GPT} != "no"
.  if ${SWAPSECTORS} != 0
	${TOOL_SED}							\
	    -e "s/ROOT.a/NAME=${BOOTDISK_UUID}/"			\
	    -e "s/ROOT.b/NAME=${SWAPDISK_UUID}/"			\
	    < ${FSTAB_IN} > ${WORKFSTAB}
.  else
	${TOOL_SED}							\
	    -e "s/ROOT.a/NAME=${BOOTDISK_UUID}/"			\
	    -e "/ROOT.b/d"						\
	    < ${FSTAB_IN} > ${WORKFSTAB}
.  endif
.else
.  if ${SWAPSECTORS} != 0
	${TOOL_SED} < ${FSTAB_IN} > ${WORKFSTAB}
.  else
	${TOOL_SED}							\
	    -e "/ROOT.b/d"						\
	    < ${FSTAB_IN} > ${WORKFSTAB}
.  endif
.endif

#
# create root file system for the image
#
${TARGETFS}: prepare_md_post ${WORKFSTAB}
	@if [ ! -d ${RELEASEDIR}/${RELEASEMACHINEDIR} ]; then 		\
		echo "Missing ${RELEASEDIR}/${RELEASEMACHINEDIR}, aborting"; \
		false; 							\
	fi;
	@${MKDIR} ${MKDIRPERM} ${WORKDIR}
.for set in ${IMG_SETS}
	@if [ ! -f ${SETS_DIR}/${set}.${TAR_SUFF} ]; then 		\
		echo "Missing ${SETS_DIR}/${set}.${TAR_SUFF}, aborting";\
		false; 							\
	fi
	@echo Extracting ${set}.${TAR_SUFF} ...
	@(cd ${WORKDIR}; ${TOOL_PAX} ${PAX_TIMESTAMP} -rn \
		--use-compress-program=${COMPRESS_PROGRAM:Q} \
		-f ${SETS_DIR}/${set}.${TAR_SUFF} .)
.endfor
.if defined(SECONDARY_BOOT)
	@echo Copying secondary boot...
	${INSTALL} ${COPY} -m 0444 ${WORKDIR}/usr/mdec/${SECONDARY_BOOT} ${WORKDIR}
.endif
	${INSTALL} ${COPY} -m 0644 ${WORKFSTAB} ${WORKDIR}/etc/fstab
	@echo Setting rc_configured=YES in /etc/rc.conf ...
	${TOOL_SED} "s/rc_configured=NO/rc_configured=YES/"		\
	    < ${WORKDIR}/etc/rc.conf > ${WORKRCCONF}
	${INSTALL} ${COPY} -m 0644 ${WORKRCCONF} ${WORKDIR}/etc/rc.conf
.if defined(IMGDIR_EXTRA)
	@echo Copying extra dirs...
.for _SRCDIR _TARGET in ${IMGDIR_EXTRA}
	@if [ ! -d ${_SRCDIR} ]; then 					\
		echo "${_SRCDIR} is not directory, aborting";		\
		false; 							\
	fi
	${MKDIR} ${MKDIRPERM} ${WORKDIR}/${_TARGET}
	(cd ${_SRCDIR} ;						\
	    ${TOOL_PAX} ${PAX_TIMESTAMP} -rw -pe -v			\
	    ${IMGDIR_EXCLUDE}						\
	    . ${.OBJDIR}/${WORKDIR}/${_TARGET} )
.endfor
.endif
.if defined(IMGFILE_EXTRA)
	@echo Copying extra files...
.for _SRC _TARGET in ${IMGFILE_EXTRA}
	@if [ ! -f ${_SRC} ]; then 					\
		echo "${_SRC} in IMGFILE_EXTRA not found, aborting";	\
		false; 							\
	fi
	@if [ -f ${_SRC} ]; then 					\
		echo ${INSTALL} ${COPY} -m 0644 ${_SRC} ${WORKDIR}/${_TARGET}; \
		${INSTALL} ${COPY} -m 0644 ${_SRC} ${WORKDIR}/${_TARGET}; \
	fi
.endfor
.endif
	@echo Preparing spec files for makefs...
	${RM} -f ${WORKSPEC}
	cat ${WORKDIR}/etc/mtree/* |					\
	    ${TOOL_SED} -e 's/ size=[0-9]*//' > ${WORKSPEC}
	${HOST_SH} ${WORKDIR}/dev/MAKEDEV -s all ipty |			\
	    ${TOOL_SED} -e '/^\. type=dir/d' -e 's,^\.,./dev,' >> ${WORKSPEC}
	cat ${SPEC_IN} >> ${WORKSPEC}
.if defined(SECONDARY_BOOT)
	echo "./${SECONDARY_BOOT} type=file uname=root gname=wheel mode=0444" \
	    >> ${WORKSPEC}
.endif
.if defined(SPEC_EXTRA)
	cat ${SPEC_EXTRA} >> ${WORKSPEC}
.endif
	@echo Creating rootfs...
	# XXX /var/spool/ftp/hidden is unreadable
	${CHMOD} +r ${WORKDIR}/var/spool/ftp/hidden
	${TOOL_MAKEFS} -M ${FSSIZE} -m ${FSSIZE}			\
	    -B ${TARGET_ENDIANNESS}					\
	    -F ${WORKSPEC} -N ${WORKDIR}/etc				\
	    ${MAKEFS_TIMESTAMP}						\
	    ${IMGMAKEFSOPTIONS}						\
	    ${WORKFS} ${WORKDIR}
.if !empty(PRIMARY_BOOT) && ${INSTALLBOOT_AFTER_DISKLABEL} == "no"
	${TOOL_INSTALLBOOT} -v -m ${MACHINE} ${INSTALLBOOTOPTIONS} ${WORKFS} \
	    ${WORKDIR}/usr/mdec/${PRIMARY_BOOT} ${SECONDARY_BOOT_ARG}
.endif
	@echo done.
	mv ${WORKFS} ${.TARGET}

TARGET_BLOCKS=
.if ${USE_MBR} != "no" || ${USE_GPT} != "no"
TARGET_BLOCKS+=	${WORKMBRTRUNC}
.endif
.if ${USE_GPT} != "no"
TARGET_BLOCKS+=	${WORKEFI}
.endif
.if ${FATSECTORS} != 0
TARGET_BLOCKS+=	${WORKFAT}
.endif
TARGET_BLOCKS+=	 ${TARGETFS}
.if ${SWAPSECTORS} != 0
TARGET_BLOCKS+=	${WORKSWAP}
.endif
.if ${USE_GPT} != "no"
TARGET_BLOCKS+=	${WORKGPT}
.endif

CLEANFILES+=	${WORKMBR} ${WORKMBRTRUNC} ${WORKSWAP}
CLEANFILES+=	${WORKEFI} ${WORKGPT}
CLEANFILES+=	${WORKFAT}
CLEANFILES+=	${WORKLABEL}.tmp ${WORKLABEL}
CLEANFILES+=	${WORKIMG} ${IMGBASE}.img

${WORKSWAP}:
.if ${SWAPSECTORS} == 0
	printf "" > ${WORKSWAP}
.else
	${DD} if=/dev/zero of=${WORKSWAP} seek=$$((${SWAPSECTORS} - 1)) count=1
.endif

${WORKMBRTRUNC}: ${WORKMBR}
	${DD} if=${WORKMBR} count=${LABELSECTORS} of=${WORKMBRTRUNC}

${WORKLABEL}:
	${TOOL_SED}							\
	    -e "s/@@SECTORS@@/${SECTORS}/"				\
	    -e "s/@@HEADS@@/${HEADS}/"					\
	    -e "s/@@SECPERCYLINDERS@@/${SECPERCYLINDERS}/"		\
	    -e "s/@@CYLINDERS@@/${CYLINDERS}/"				\
	    -e "s/@@IMAGESECTORS@@/${IMAGESECTORS}/"			\
	    -e "s/@@FSSECTORS@@/${FSSECTORS}/"				\
	    -e "s/@@FSOFFSET@@/${FSOFFSET}/"				\
	    -e "s/@@SWAPSECTORS@@/${SWAPSECTORS}/"			\
	    -e "s/@@SWAPOFFSET@@/${SWAPOFFSET}/"			\
	    -e "s/@@BSDPARTSECTORS@@/${BSDPARTSECTORS}/"		\
	    -e "s/@@FATSECTORS@@/${FATSECTORS}/"			\
	    -e "s/@@FATOFFSET@@/${FATOFFSET}/"				\
	    < ${DISKPROTO_IN} > ${WORKLABEL}.tmp
	mv ${WORKLABEL}.tmp ${WORKLABEL}

${WORKEFI}: ${TARGETFS}
	@echo create EFI system partition...
	@${MKDIR} ${MKDIRPERM} ${EFIWORKDIR}/EFI/boot
.for f in ${EFIBOOT}
	${INSTALL} ${COPY} -m 0644 ${f} ${EFIWORKDIR}/EFI/boot/`basename ${f}`
.endfor
	${RM} -f ${WORKEFI}
	${TOOL_MAKEFS} -M ${EFIMB}m -m ${EFIMB}m			\
	    -B ${TARGET_ENDIANNESS}					\
	    ${MAKEFS_TIMESTAMP}						\
	    -t msdos -o F=32,c=1					\
	    ${WORKEFI} ${EFIWORKDIR}

${WORKFAT}: ${TARGETFS} ${FATFILES}
	@echo create FAT partition for bootstrap files...
	@${MKDIR} ${MKDIRPERM} ${WORKFATDIR}
.if defined(FATFILES)
	@echo Copying files for FAT partition...
.for f in ${FATFILES}
	@if [ ! -f ${f} ]; then 					\
		echo "${f} in FATFILES not found, aborting";		\
		false; 							\
	fi
	${INSTALL} ${COPY} -m 0644 ${f} ${WORKFATDIR}
.endfor
.endif
	${RM} -f ${WORKFAT}
	${TOOL_MAKEFS} -M ${FATMB}m -m ${FATMB}m			\
	    ${MAKEFS_TIMESTAMP}						\
	    -t msdos							\
	    ${FATMAKEFSOPTIONS}						\
	    ${WORKFAT} ${WORKFATDIR}

${WORKMBR}:
.if ${USE_GPT} != "no"
	@echo creating GPT header and partition entries...
	${RM} -f ${WORKMBR}
	${DD} if=/dev/zero of=${WORKMBR} seek=$$((${IMAGESECTORS} - 1)) count=1
	${TOOL_GPT} ${GPT_TIMESSTAMP} ${WORKMBR} create
	${TOOL_GPT} ${GPT_TIMESSTAMP} ${WORKMBR} add -a 1m -s ${EFISECTORS} -t efi -l "EFI system"
	${TOOL_GPT} ${GPT_TIMESSTAMP} ${WORKMBR} add -a 1m -s ${FSSECTORS} -t ffs
.  if ${SWAPSECTORS} != 0
	${TOOL_GPT} ${GPT_TIMESSTAMP} ${WORKMBR} add -a 1m -s ${SWAPSECTORS} -t swap
.  endif
.elif ${USE_MBR} != "no"
	@echo creating MBR labels...
	${DD} if=/dev/zero of=${WORKMBR} seek=$$((${IMAGESECTORS} - 1)) count=1
.  if ${FATSECTORS} != 0
	# create BSD partition
	${TOOL_FDISK} -f -i -u \
	    -b ${MBRCYLINDERS}/${MBRHEADS}/${MBRSECTORS}		\
	    -1 -a -s ${MBRNETBSD}/${FSOFFSET}/${BSDPARTSECTORS}		\
	    -F ${WORKMBR}
	# create FAT partition
	${TOOL_FDISK} -f -u \
	    -b ${MBRCYLINDERS}/${MBRHEADS}/${MBRSECTORS}		\
	    -0 -s ${MBRFAT}/${FATOFFSET}/${FATSECTORS}			\
	    -F ${WORKMBR}
.  else
	# create BSD partition
	${TOOL_FDISK} -f -i -u \
	    -b ${MBRCYLINDERS}/${MBRHEADS}/${MBRSECTORS}		\
	    -0 -a -s ${MBRNETBSD}/${FSOFFSET}/${BSDPARTSECTORS}		\
	    -F ${WORKMBR}
.  endif
.  if empty(MBR_BOOTCODE)
	@if [ -f ${WORKDIR}/usr/mdec/${MBR_DEFAULT_BOOTCODE} ]; then 	\
		echo ${TOOL_FDISK} -f					\
		    -i -c ${WORKDIR}/usr/mdec/${MBR_DEFAULT_BOOTCODE}	\
		    -F ${WORKMBR};					\
		${TOOL_FDISK} -f					\
		    -i -c ${WORKDIR}/usr/mdec/${MBR_DEFAULT_BOOTCODE}	\
		    -F ${WORKMBR};					\
	fi
.  else
	@if [ ! -f ${WORKDIR}/usr/mdec/${MBR_BOOTCODE} ]; then 		\
		echo "${MBR_BOOTCODE} is not found in DESTDIR/usr/mdec, aborting"; \
		false; 							\
	fi
	${TOOL_FDISK} -f -i -c ${WORKDIR}/usr/mdec/${MBR_BOOTCODE}	\
	    -F ${WORKMBR}
.  endif
.endif

${WORKGPT}: ${WORKMBR}
	@echo create GPT image...
	${DD} if=${WORKMBR} of=${WORKGPT} skip=$$((${IMAGESECTORS} - 2048)) \
	    count=2048

.if ${USE_GPT} == "no"
${IMGBASE}.img:	${WORKLABEL}
.endif

${IMGBASE}.img:	${TARGET_BLOCKS}
	${CAT} ${TARGET_BLOCKS} > ${WORKIMG}
.if ${USE_GPT} != "no"
	${TOOL_GPT} ${GPT_TIMESSTAMP} ${WORKIMG} biosboot -i 2		\
	    -c ${.OBJDIR}/${WORKDIR}/usr/mdec/gptmbr.bin
	${TOOL_GPT} ${GPT_TIMESSTAMP} ${WORKIMG} set -a bootme -i 2
.endif	# USE_GPT != "no"
.if ${USE_SUNLABEL} != "no"
	@echo Creating sun disklabel...
	printf 'V ncyl %d\nV nhead %d\nV nsect %d\na %d %d/0/0\nb %d %d/0/0\nW\n' \
	    ${CYLINDERS} ${HEADS} ${SECTORS} \
	    ${FSOFFSET} ${FSCYLINDERS} ${FSCYLINDERS} ${SWAPCYLINDERS} | \
	    ${TOOL_SUNLABEL} -nq ${WORKIMG}
.endif
.if ${USE_GPT} == "no"
	${TOOL_DISKLABEL} -R -F ${WORKIMG} ${WORKLABEL}
.if !empty(PRIMARY_BOOT) && ${INSTALLBOOT_AFTER_DISKLABEL} != "no"
	${TOOL_INSTALLBOOT} -v -m ${MACHINE} ${INSTALLBOOTOPTIONS} ${WORKIMG} \
	    ${WORKDIR}/usr/mdec/${PRIMARY_BOOT}
.endif
.endif
	mv ${WORKIMG} ${.TARGET}

CLEANFILES+=	${IMGBASE}.img.gz ${IMGBASE}.img.gz.tmp

${IMGBASE}.img.gz:	${IMGBASE}.img
	${TOOL_GZIP_N} -9c ${IMGBASE}.img > ${.TARGET}.tmp
	mv ${.TARGET}.tmp ${.TARGET}

clean:
	@if [ -d ${WORKDIR}/var/spool/ftp/hidden ]; then 		\
		${CHMOD} +r ${WORKDIR}/var/spool/ftp/hidden;		\
	fi	# XXX
	${RM} -fr ${WORKDIR}
	${RM} -fr ${EFIWORKDIR}
	${RM} -fr ${WORKFATDIR}

prepare_md_post: .PHONY
image_md_post: .PHONY
image_md_pre: .PHONY

.include <bsd.prog.mk>