blob: 510da39c93b739684a864dbaa65e0dc5a49180a3 [file] [log] [blame]
Paul Fagerburg5f794bf2020-02-12 13:01:36 -07001#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# Copyright 2020 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6"""Add a new variant to the Kconfig and Kconfig.name for the baseboard
7
8To start a new variant of an existing baseboard, we need to add
9the variant into the Kconfig and Kconfig.name files for the
10baseboard. In Kconfig, we have two sections that need additional
11entries, MAINBOARD_PART_NUMBER and VARIANT_DIR.
12
13The MAINBOARD_PART_NUMBER and VARIANT_DIR just use various
14capitalizations of the variant name to create the strings.
15
16Kconfig.name adds an entire section for the new variant, and all
17of these use various capitalizations of the variant name. The strings
18in this section are SOC-specific, so we'll need versions for each
19SOC that we support.
20"""
21
22from __future__ import print_function
23import argparse
24import sys
25
26
27def main(argv):
28 """Add strings to coreboot Kconfig and Kconfig.name for a new variant
29
30 Args:
31 argv: Command-line arguments
32 """
33 parser = argparse.ArgumentParser(description=__doc__)
34 parser.add_argument('--board', type=str, required=True,
Paul Fagerburgf3fe9e82020-05-21 17:13:09 -060035 choices=('hatch', 'volteer', 'trembyle', 'dalboz',
Paul Fagerburg3ef1cbb2020-06-02 16:38:36 -060036 'waddledee', 'waddledoo', 'puff'),
Paul Fagerburg5f794bf2020-02-12 13:01:36 -070037 help='Name of the baseboard')
38 parser.add_argument('--variant', type=str, required=True,
39 help='Name of the board variant')
40 opts = parser.parse_args(argv)
41
42 add_to_kconfig(opts.variant)
43 add_to_kconfig_name(opts.board, opts.variant)
44 return True
45
46
47def add_to_kconfig(variant_name):
48 """Add options for the variant to the Kconfig
49
50 Open the Kconfig file and read it line-by-line. When we detect that we're
51 in one of the sections of interest, wait until we get a blank line
52 (signalling the end of that section), and then add our new line before
53 the blank line. The updated lines are written out to Kconfig.new in the
54 same directory as Kconfig.
55
56 Args:
57 variant_name: The name of the board variant, e.g. 'kohaku'
58 """
59 # These are the part of the strings that we'll add to the sections.
60 BOARD = 'BOARD_GOOGLE_' + variant_name.upper()
61 lowercase = variant_name.lower()
62 capitalized = lowercase.capitalize()
63
64 # These flags track if we're in a section where we need to add an option.
65 in_mainboard_part_number = False
66 in_variant_dir = False
67
68 inputname = 'Kconfig'
69 outputname = 'Kconfig.new'
70 with open(outputname, 'w', encoding='utf-8') as outfile, \
71 open(inputname, 'r', encoding='utf-8') as infile:
72 for rawline in infile:
73 line = rawline.rstrip()
74
75 # Are we in one of the sections of interest?
76 if line == 'config MAINBOARD_PART_NUMBER':
77 in_mainboard_part_number = True
78 if line == 'config VARIANT_DIR':
79 in_variant_dir = True
80
81 # Are we at the end of a section, and if so, is it one of the
82 # sections of interest?
83 if not line:
84 if in_mainboard_part_number:
85 print(f'\tdefault "{capitalized}" if {BOARD}',
86 file=outfile)
87 in_mainboard_part_number = False
88 if in_variant_dir:
89 print(f'\tdefault "{lowercase}" if {BOARD}',
90 file=outfile)
91 in_variant_dir = False
92
93 print(line, file=outfile)
94
95
96def add_to_kconfig_name(baseboard_name, variant_name):
97 """Add a config section for the variant to the Kconfig.name
98
99 Kconfig.name is easier to modify than Kconfig; it only has a block at
100 the end with the new variant's details.
101
102 Args:
103 baseboard_name: The name of the baseboard, e.g. 'hatch'
104 We expect the caller to have checked that it is one
105 that we support
106 variant_name: The name of the board variant, e.g. 'kohaku'
107 """
108 # Board name for the config section.
109 uppercase = variant_name.upper()
110 capitalized = variant_name.lower().capitalize()
111
112 inputname = 'Kconfig.name'
113 outputname = 'Kconfig.name.new'
114 with open(outputname, 'w', encoding='utf-8') as outfile, \
115 open(inputname, 'r', encoding='utf-8') as infile:
116 # Copy all input lines to output.
117 for rawline in infile:
118 line = rawline.rstrip()
119 print(line, file=outfile)
120
121 # Now add the new section.
122 if baseboard_name == 'hatch':
123 print('\nconfig ' + 'BOARD_GOOGLE_' + uppercase, file=outfile)
124 print('\tbool "-> ' + capitalized + '"', file=outfile)
125 print('\tselect BOARD_GOOGLE_BASEBOARD_HATCH', file=outfile)
126 print('\tselect BOARD_ROMSIZE_KB_16384', file=outfile)
127 elif baseboard_name == 'volteer':
128 print('\nconfig ' + 'BOARD_GOOGLE_' + uppercase, file=outfile)
129 print('\tbool "-> ' + capitalized + '"', file=outfile)
130 print('\tselect BOARD_GOOGLE_BASEBOARD_VOLTEER', file=outfile)
131 elif baseboard_name == 'trembyle':
132 print('\nconfig ' + 'BOARD_GOOGLE_' + uppercase, file=outfile)
133 print('\tbool "-> ' + capitalized + '"', file=outfile)
134 print('\tselect BOARD_GOOGLE_BASEBOARD_TREMBYLE', file=outfile)
Paul Fagerburg43a7d892020-05-19 20:28:11 -0600135 elif baseboard_name == 'dalboz':
136 print('\nconfig ' + 'BOARD_GOOGLE_' + uppercase, file=outfile)
137 print('\tbool "-> ' + capitalized + '"', file=outfile)
138 print('\tselect BOARD_GOOGLE_BASEBOARD_DALBOZ', file=outfile)
Paul Fagerburgf3fe9e82020-05-21 17:13:09 -0600139 elif baseboard_name == 'waddledee':
140 print('\nconfig ' + 'BOARD_GOOGLE_' + uppercase, file=outfile)
141 print('\tbool "-> ' + capitalized + '"', file=outfile)
Paul Fagerburg5a260942021-05-04 12:22:41 -0600142 print('\tselect BOARD_GOOGLE_BASEBOARD_DEDEDE_CR50', file=outfile)
Paul Fagerburgf3fe9e82020-05-21 17:13:09 -0600143 print('\tselect BASEBOARD_DEDEDE_LAPTOP', file=outfile)
Paul Fagerburgf3fe9e82020-05-21 17:13:09 -0600144 elif baseboard_name == 'waddledoo':
145 print('\nconfig ' + 'BOARD_GOOGLE_' + uppercase, file=outfile)
146 print('\tbool "-> ' + capitalized + '"', file=outfile)
Paul Fagerburg5a260942021-05-04 12:22:41 -0600147 print('\tselect BOARD_GOOGLE_BASEBOARD_DEDEDE_CR50', file=outfile)
Paul Fagerburgf3fe9e82020-05-21 17:13:09 -0600148 print('\tselect BASEBOARD_DEDEDE_LAPTOP', file=outfile)
Paul Fagerburgf3fe9e82020-05-21 17:13:09 -0600149 print('\tselect DRIVERS_GENERIC_MAX98357A', file=outfile)
150 print('\tselect DRIVERS_I2C_DA7219', file=outfile)
Paul Fagerburg3ef1cbb2020-06-02 16:38:36 -0600151 elif baseboard_name == 'puff':
152 print('\nconfig ' + 'BOARD_GOOGLE_' + uppercase, file=outfile)
153 print('\tbool "-> ' + capitalized + '"', file=outfile)
154 print('\tselect BOARD_GOOGLE_BASEBOARD_PUFF', file=outfile)
Paul Fagerburg5f794bf2020-02-12 13:01:36 -0700155 else:
156 raise ValueError(f'Unsupported board {baseboard_name}')
157
158
159if __name__ == '__main__':
160 sys.exit(not int(main(sys.argv[1:])))