11.1Stsutsui#	$NetBSD: diskproto2sunlabel.awk,v 1.1 2026/01/13 14:09:02 tsutsui Exp $
21.1Stsutsui#
31.1Stsutsui# Copyright (c) 2026 Izumi Tsutsui.  All rights reserved.
41.1Stsutsui#
51.1Stsutsui# Redistribution and use in source and binary forms, with or without
61.1Stsutsui# modification, are permitted provided that the following conditions
71.1Stsutsui# are met:
81.1Stsutsui# 1. Redistributions of source code must retain the above copyright
91.1Stsutsui#    notice, this list of conditions and the following disclaimer.
101.1Stsutsui# 2. Redistributions in binary form must reproduce the above copyright
111.1Stsutsui#    notice, this list of conditions and the following disclaimer in the
121.1Stsutsui#    documentation and/or other materials provided with the distribution.
131.1Stsutsui#
141.1Stsutsui# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
151.1Stsutsui# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
161.1Stsutsui# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
171.1Stsutsui# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
181.1Stsutsui# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
191.1Stsutsui# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
201.1Stsutsui# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
211.1Stsutsui# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
221.1Stsutsui# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
231.1Stsutsui# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
241.1Stsutsui
251.1Stsutsui#
261.1Stsutsui# diskproto2sunlabel.awk
271.1Stsutsui#
281.1Stsutsui# Read a disklabel prototype (${WORKLABEL} generated by Makefile.bootimage),
291.1Stsutsui# and emit sunlabel(8) command script to stdout.
301.1Stsutsui#
311.1Stsutsui# Usage:
321.1Stsutsui#   awk -f diskproto2sunlabel.awk < ${WORKLABEL} | sunlabel -nq ${WORKIMG}
331.1Stsutsui
341.1Stsutsuifunction err(msg) {
351.1Stsutsui	print "diskproto2sunlabel.awk: " msg > "/dev/stderr"
361.1Stsutsui	exit 1
371.1Stsutsui}
381.1Stsutsui
391.1StsutsuiBEGIN {
401.1Stsutsui	# Assume MAXPARTITIONS==8 is default on ports that require sunlabel(8)
411.1Stsutsui	if (parts == "")
421.1Stsutsui		parts = "a b c d e f g h"
431.1Stsutsui	n = split(parts, pstr, /[ \t]+/)
441.1Stsutsui	for (i = 1; i <= n; i++)
451.1Stsutsui		validpart[pstr[i]] = 1
461.1Stsutsui	nparts = 0
471.1Stsutsui}
481.1Stsutsui
491.1Stsutsui# remove comments and leading/trailing spaces/tabs from all lines
501.1Stsutsui{
511.1Stsutsui	sub(/[ \t]*#.*/, "", $0)
521.1Stsutsui	gsub(/^[ \t]+/, "", $0)
531.1Stsutsui	gsub(/[ \t]+$/, "", $0)
541.1Stsutsui	if ($0 == "")
551.1Stsutsui		next
561.1Stsutsui}
571.1Stsutsui
581.1Stsutsui/^sectors\/track:[ \t]*[0-9]+/ {
591.1Stsutsui	if (NF != 2)
601.1Stsutsui		err("Invalid sectors/track line")
611.1Stsutsui	spt = $2
621.1Stsutsui	next
631.1Stsutsui}
641.1Stsutsui
651.1Stsutsui/^tracks\/cylinder:[ \t]*[0-9]+/ {
661.1Stsutsui	if (NF != 2)
671.1Stsutsui		err("Invalid tracks/cylinder line")
681.1Stsutsui	head = $2
691.1Stsutsui	next
701.1Stsutsui}
711.1Stsutsui
721.1Stsutsui/^sectors\/cylinder:[ \t]*[0-9]+/ {
731.1Stsutsui	if (NF != 2)
741.1Stsutsui		err("Invalid sectors/cylinder line")
751.1Stsutsui	spc = $2
761.1Stsutsui	next
771.1Stsutsui}
781.1Stsutsui
791.1Stsutsui/^cylinders:[ \t]*[0-9]+/ {
801.1Stsutsui	if (NF != 2)
811.1Stsutsui		err("Invalid cylinders line")
821.1Stsutsui	ncyl = $2
831.1Stsutsui	next
841.1Stsutsui}
851.1Stsutsui
861.1Stsutsui/^[a-p]:[ \t]*[0-9]+[ \t]+[0-9]+/ {
871.1Stsutsui	part = $1
881.1Stsutsui	sub(/:$/, "", part)
891.1Stsutsui	part_size[part] = $2
901.1Stsutsui	part_offset[part] = $3
911.1Stsutsui	parts_in_image[nparts++] = part
921.1Stsutsui	next
931.1Stsutsui}
941.1Stsutsui
951.1Stsutsui# ignore all other lines; assume ${WORKLABEL} is a generated file here
961.1Stsutsui{
971.1Stsutsui	next
981.1Stsutsui}
991.1Stsutsui
1001.1StsutsuiEND {
1011.1Stsutsui	if (spt == "")
1021.1Stsutsui		err("missing geometry (sectors/track)")
1031.1Stsutsui	if (head == "")
1041.1Stsutsui		err("missing geometry (tracks/cylinder)")
1051.1Stsutsui	if (ncyl == "")
1061.1Stsutsui		err("missing geometry (cylinders)")
1071.1Stsutsui	if (spc == "")
1081.1Stsutsui		err("missing geometry (sectors/cylinder)")
1091.1Stsutsui	if (spc != spt * head)
1101.1Stsutsui		err("inconsistent sectors/track and sectors/cylinder")
1111.1Stsutsui	if (nparts == 0)
1121.1Stsutsui		err("no valid partition")
1131.1Stsutsui
1141.1Stsutsui	print "V ncyl " ncyl
1151.1Stsutsui	print "V nhead " head
1161.1Stsutsui	print "V nsect " spt
1171.1Stsutsui
1181.1Stsutsui	for (i = 0; i < nparts; i++) {
1191.1Stsutsui		p = parts_in_image[i]
1201.1Stsutsui		if (!validpart[p])
1211.1Stsutsui			continue
1221.1Stsutsui		if (part_size[p] == 0)
1231.1Stsutsui			continue
1241.1Stsutsui
1251.1Stsutsui		# sunlabel partition start must be cylinder-aligned.
1261.1Stsutsui		if ((part_offset[p] % spc) != 0)
1271.1Stsutsui			err("partition " p \
1281.1Stsutsui			    " offset " part_offset[p] \
1291.1Stsutsui			    " is not cylinder-aligned (spc=" spc ")")
1301.1Stsutsui
1311.1Stsutsui		start = int(part_offset[p] / spc)
1321.1Stsutsui		print p " " start " " part_size[p]
1331.1Stsutsui	}
1341.1Stsutsui
1351.1Stsutsui	print "W"
1361.1Stsutsui}
137