Skip to content

Commit a6e1b8c

Browse files
committed
Add CLI flag to allow setting of OpenFirmware environment variables
Allows enabling of verbose booting via the command-line interface (instead of having to drop into the interactive debugger).
1 parent 6e74146 commit a6e1b8c

4 files changed

Lines changed: 47 additions & 12 deletions

File tree

debugger/debugger.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -784,9 +784,15 @@ void DppcDebugger::enter_debugger() {
784784
ss >> var_name;
785785
std::istream::sentry se(ss); // skip white space
786786
getline(ss, value); // get everything up to eol
787-
if (ofnvram->init())
787+
if (ofnvram->init()) {
788+
cout << " Cannot open NVRAM" << endl;
788789
continue;
789-
ofnvram->setenv(var_name, value);
790+
}
791+
if (!ofnvram->setenv(var_name, value)) {
792+
cout << " Please try again" << endl;
793+
} else {
794+
cout << " ok" << endl; // mimic Forth
795+
}
790796
#ifndef _WIN32
791797
} else if (cmd == "nvedit") {
792798
cmd = "";

devices/common/ofnvram.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,9 @@ bool OfConfigChrp::validate()
322322
bool wip = true;
323323
bool of_part_found = false;
324324

325+
if (this->nvram_obj == nullptr)
326+
return false;
327+
325328
// search the entire 8KB NVRAM for CHRP OF config partition.
326329
// Bail out if an unknown partition or free space is encountered.
327330
// Skip over known partitions.
@@ -592,16 +595,12 @@ void OfConfigUtils::printenv() {
592595
}
593596
}
594597

595-
void OfConfigUtils::setenv(string var_name, string value)
598+
bool OfConfigUtils::setenv(string var_name, string value)
596599
{
597600
if (!this->open_container())
598-
return;
601+
return false;
599602

600603
OfConfigImpl::config_dict vars = this->cfg_impl->get_config_vars();
601604

602-
if (!this->cfg_impl->set_var(var_name, value)) {
603-
cout << " Please try again" << endl;
604-
} else {
605-
cout << " ok" << endl; // mimic Forth
606-
}
605+
return this->cfg_impl->set_var(var_name, value);
607606
}

devices/common/ofnvram.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ class OfConfigUtils {
134134

135135
int init();
136136
void printenv();
137-
void setenv(std::string var_name, std::string value);
137+
bool setenv(std::string var_name, std::string value);
138138

139139
protected:
140140
bool open_container();

main.cpp

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
2828
#include <cpu/ppc/ppcemu.h>
2929
#include <cpu/ppc/ppcmmu.h>
3030
#include <debugger/debugger.h>
31+
#include <devices/common/ofnvram.h>
3132
#include <machines/machinebase.h>
3233
#include <machines/machinefactory.h>
3334
#include <utils/profiler.h>
@@ -94,6 +95,7 @@ const WorkingDirectoryValidator WorkingDirectory;
9495

9596
void run_machine(
9697
std::string machine_str, char *rom_data, size_t rom_size, uint32_t execution_mode
98+
,const std::vector<std::string> &env_vars
9799
,uint32_t profiling_interval_ms
98100
);
99101

@@ -110,7 +112,7 @@ int main(int argc, char** argv) {
110112
string keyboard_string = "Eng_USA";
111113

112114
const std::map<std::string, int> kbd_map{
113-
{"Eng_USA", 0}, {"Eng_GBR", 1}, {"Fra_FRA", 10}, {"Deu_DEU", 20},
115+
{"Eng_USA", 0}, {"Eng_GBR", 1}, {"Fra_FRA", 10}, {"Deu_DEU", 20},
114116
{"Ita_ITA", 30},{"Spa_ESP", 40}, {"Jpn_JPN", 80},
115117
};
116118

@@ -142,6 +144,10 @@ int main(int argc, char** argv) {
142144
app.add_flag("--log-no-uptime", log_no_uptime,
143145
"Disable the uptime preamble of logged messages");
144146

147+
std::vector<std::string> env_vars;
148+
app.add_option("--setenv", env_vars, "Set OpenFirmware variables at startup")
149+
->take_all();
150+
145151
uint32_t profiling_interval_ms = 0;
146152
#ifdef CPU_PROFILING
147153
app.add_option("--profiling-interval-ms", profiling_interval_ms,
@@ -280,6 +286,7 @@ int main(int argc, char** argv) {
280286
&rom_data[0],
281287
rom_size,
282288
execution_mode,
289+
env_vars,
283290
profiling_interval_ms);
284291
if (power_off_reason == po_restarting) {
285292
LOG_F(INFO, "Restarting...");
@@ -306,6 +313,7 @@ int main(int argc, char** argv) {
306313
void run_machine(std::string machine_str, char* rom_data,
307314
size_t rom_size,
308315
uint32_t execution_mode,
316+
const std::vector<std::string> &env_vars,
309317
uint32_t
310318
#ifdef CPU_PROFILING
311319
profiling_interval_ms
@@ -315,6 +323,28 @@ void run_machine(std::string machine_str, char* rom_data,
315323
return;
316324
}
317325

326+
if (!env_vars.empty()) {
327+
OfConfigUtils ofnvram;
328+
if (!ofnvram.init()) {
329+
for (const auto &env_var : env_vars) {
330+
auto pos = env_var.find('=');
331+
if (pos != std::string::npos) {
332+
std::string name = env_var.substr(0, pos);
333+
std::string value = env_var.substr(pos + 1);
334+
if (ofnvram.setenv(name, value)) {
335+
LOG_F(INFO, "Set OpenFirmware variable %ss to %s", name.c_str(), value.c_str());
336+
} else {
337+
LOG_F(WARNING, "Cannot set OpenFirmware variable %s to %s", name.c_str(), value.c_str());
338+
}
339+
} else {
340+
LOG_F(WARNING, "Invalid format for --setenv: %s", env_var.c_str());
341+
}
342+
}
343+
} else {
344+
LOG_F(WARNING, "Cannot initialize NVRAM wrapper, will not set OpenFirmware variables");
345+
}
346+
}
347+
318348
uint32_t deterministic_timer;
319349
if (is_deterministic) {
320350
EventManager::get_instance()->disable_input_handlers();
@@ -332,7 +362,7 @@ void run_machine(std::string machine_str, char* rom_data,
332362

333363
// set up system wide event polling using
334364
// default Macintosh polling rate of 11 ms
335-
uint32_t event_timer = TimerManager::get_instance()->add_cyclic_timer(MSECS_TO_NSECS(11), [] {
365+
uint32_t event_timer = TimerManager::get_instance()->add_cyclic_timer(MSECS_TO_NSECS(11), [] {
336366
EventManager::get_instance()->poll_events(keyboard_id);
337367
});
338368

0 commit comments

Comments
 (0)