Replace CONFIG_THREAD_OPTIONROMS with a runtime config setting.

Replace the CONFIG_THREAD_OPTIONROMS option with the CBFS (or fw_cfg)
file "etc/threads".  This allows for the "threads during optionrom"
capability to be enabled/disabled without requiring SeaBIOS to be
recompiled.  A value of "2" in this file will enable threads to run
during option rom execution.

This change also allows for all threads to be disabled via the same
runtime config file.  Setting the file to a value of "0" will cause
SeaBIOS to perform all hardware initialization serially.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
diff --git a/src/Kconfig b/src/Kconfig
index cce3ad8..a863866 100644
--- a/src/Kconfig
+++ b/src/Kconfig
@@ -46,17 +46,6 @@
         default y
         help
             Support running hardware initialization in parallel.
-    config THREAD_OPTIONROMS
-        depends on THREADS && !CSM
-        bool "Hardware init during option ROM execution"
-        default n
-        help
-            Allow hardware init to run in parallel with optionrom execution.
-
-            This can reduce boot time, but can cause some timing
-            variations during option ROM code execution.  It is not
-            known if all option ROMs will behave properly with this
-            option.
 
     config RELOCATE_INIT
         bool "Copy init code to high memory"
diff --git a/src/post.c b/src/post.c
index b5cbdb8..5fc1968 100644
--- a/src/post.c
+++ b/src/post.c
@@ -122,6 +122,7 @@
     bda_init();
 
     // Other interfaces
+    thread_init();
     boot_init();
     bios32_init();
     pmm_init();
@@ -212,15 +213,15 @@
     // Setup platform devices.
     platform_hardware_setup();
 
-    // Start hardware initialization (if optionrom threading)
-    if (CONFIG_THREAD_OPTIONROMS)
+    // Start hardware initialization (if threads allowed during optionroms)
+    if (threads_during_optionroms())
         device_hardware_setup();
 
     // Run vga option rom
     vgarom_setup();
 
     // Do hardware initialization (if running synchronously)
-    if (!CONFIG_THREAD_OPTIONROMS) {
+    if (!threads_during_optionroms()) {
         device_hardware_setup();
         wait_threads();
     }
diff --git a/src/stacks.c b/src/stacks.c
index de71c1d..6bcb319 100644
--- a/src/stacks.c
+++ b/src/stacks.c
@@ -10,6 +10,7 @@
 #include "list.h" // hlist_node
 #include "malloc.h" // free
 #include "output.h" // dprintf
+#include "romfile.h" // romfile_loadint
 #include "stacks.h" // struct mutex_s
 #include "util.h" // useRTC
 
@@ -271,6 +272,24 @@
     return (void*)ALIGN_DOWN(esp, THREADSTACKSIZE);
 }
 
+static int ThreadControl;
+
+// Initialize the support for internal threads.
+void
+thread_init(void)
+{
+    if (! CONFIG_THREADS)
+        return;
+    ThreadControl = romfile_loadint("etc/threads", 1);
+}
+
+// Should hardware initialization threads run during optionrom execution.
+int
+threads_during_optionroms(void)
+{
+    return CONFIG_THREADS && ThreadControl == 2;
+}
+
 // Switch to next thread stack.
 static void
 switch_next(struct thread_info *cur)
@@ -309,7 +328,7 @@
 run_thread(void (*func)(void*), void *data)
 {
     ASSERT32FLAT();
-    if (! CONFIG_THREADS)
+    if (! CONFIG_THREADS || ! ThreadControl)
         goto fail;
     struct thread_info *thread;
     thread = memalign_tmphigh(THREADSTACKSIZE, THREADSTACKSIZE);
@@ -452,7 +471,7 @@
 void
 start_preempt(void)
 {
-    if (! CONFIG_THREAD_OPTIONROMS)
+    if (! threads_during_optionroms())
         return;
     CanPreempt = 1;
     PreemptCount = 0;
@@ -463,7 +482,7 @@
 void
 finish_preempt(void)
 {
-    if (! CONFIG_THREAD_OPTIONROMS) {
+    if (! threads_during_optionroms()) {
         yield();
         return;
     }
@@ -477,7 +496,7 @@
 int
 wait_preempt(void)
 {
-    if (MODESEGMENT || !CONFIG_THREAD_OPTIONROMS || !CanPreempt
+    if (MODESEGMENT || !CONFIG_THREADS || !CanPreempt
         || getesp() < MAIN_STACK_MAX)
         return 0;
     while (CanPreempt)
@@ -498,7 +517,7 @@
 check_preempt(void)
 {
     extern void _cfunc32flat_yield_preempt(void);
-    if (CONFIG_THREAD_OPTIONROMS && GET_GLOBAL(CanPreempt) && have_threads())
+    if (CONFIG_THREADS && GET_GLOBAL(CanPreempt) && have_threads())
         call32(_cfunc32flat_yield_preempt, 0, 0);
 }
 
diff --git a/src/stacks.h b/src/stacks.h
index 9fe8761..d8584f2 100644
--- a/src/stacks.h
+++ b/src/stacks.h
@@ -21,6 +21,8 @@
 struct thread_info *getCurThread(void);
 void yield(void);
 void yield_toirq(void);
+void thread_init(void);
+int threads_during_optionroms(void);
 void run_thread(void (*func)(void*), void *data);
 void wait_threads(void);
 struct mutex_s { u32 isLocked; };