diskproto2sunlabel.awk revision 1.1
1#	$NetBSD: diskproto2sunlabel.awk,v 1.1 2026/01/13 14:09:02 tsutsui Exp $
2#
3# Copyright (c) 2026 Izumi Tsutsui.  All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions
7# are met:
8# 1. Redistributions of source code must retain the above copyright
9#    notice, this list of conditions and the following disclaimer.
10# 2. Redistributions in binary form must reproduce the above copyright
11#    notice, this list of conditions and the following disclaimer in the
12#    documentation and/or other materials provided with the distribution.
13#
14# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24
25#
26# diskproto2sunlabel.awk
27#
28# Read a disklabel prototype (${WORKLABEL} generated by Makefile.bootimage),
29# and emit sunlabel(8) command script to stdout.
30#
31# Usage:
32#   awk -f diskproto2sunlabel.awk < ${WORKLABEL} | sunlabel -nq ${WORKIMG}
33
34function err(msg) {
35	print "diskproto2sunlabel.awk: " msg > "/dev/stderr"
36	exit 1
37}
38
39BEGIN {
40	# Assume MAXPARTITIONS==8 is default on ports that require sunlabel(8)
41	if (parts == "")
42		parts = "a b c d e f g h"
43	n = split(parts, pstr, /[ \t]+/)
44	for (i = 1; i <= n; i++)
45		validpart[pstr[i]] = 1
46	nparts = 0
47}
48
49# remove comments and leading/trailing spaces/tabs from all lines
50{
51	sub(/[ \t]*#.*/, "", $0)
52	gsub(/^[ \t]+/, "", $0)
53	gsub(/[ \t]+$/, "", $0)
54	if ($0 == "")
55		next
56}
57
58/^sectors\/track:[ \t]*[0-9]+/ {
59	if (NF != 2)
60		err("Invalid sectors/track line")
61	spt = $2
62	next
63}
64
65/^tracks\/cylinder:[ \t]*[0-9]+/ {
66	if (NF != 2)
67		err("Invalid tracks/cylinder line")
68	head = $2
69	next
70}
71
72/^sectors\/cylinder:[ \t]*[0-9]+/ {
73	if (NF != 2)
74		err("Invalid sectors/cylinder line")
75	spc = $2
76	next
77}
78
79/^cylinders:[ \t]*[0-9]+/ {
80	if (NF != 2)
81		err("Invalid cylinders line")
82	ncyl = $2
83	next
84}
85
86/^[a-p]:[ \t]*[0-9]+[ \t]+[0-9]+/ {
87	part = $1
88	sub(/:$/, "", part)
89	part_size[part] = $2
90	part_offset[part] = $3
91	parts_in_image[nparts++] = part
92	next
93}
94
95# ignore all other lines; assume ${WORKLABEL} is a generated file here
96{
97	next
98}
99
100END {
101	if (spt == "")
102		err("missing geometry (sectors/track)")
103	if (head == "")
104		err("missing geometry (tracks/cylinder)")
105	if (ncyl == "")
106		err("missing geometry (cylinders)")
107	if (spc == "")
108		err("missing geometry (sectors/cylinder)")
109	if (spc != spt * head)
110		err("inconsistent sectors/track and sectors/cylinder")
111	if (nparts == 0)
112		err("no valid partition")
113
114	print "V ncyl " ncyl
115	print "V nhead " head
116	print "V nsect " spt
117
118	for (i = 0; i < nparts; i++) {
119		p = parts_in_image[i]
120		if (!validpart[p])
121			continue
122		if (part_size[p] == 0)
123			continue
124
125		# sunlabel partition start must be cylinder-aligned.
126		if ((part_offset[p] % spc) != 0)
127			err("partition " p \
128			    " offset " part_offset[p] \
129			    " is not cylinder-aligned (spc=" spc ")")
130
131		start = int(part_offset[p] / spc)
132		print p " " start " " part_size[p]
133	}
134
135	print "W"
136}
137