elfloader: remove loading cantrip-builtins

Bug: 294433731

Change-Id: Ie320618b920c9d6aa456721eb0d4dbf940d0c373
diff --git a/capsules/src/elfloader_capsule.rs b/capsules/src/elfloader_capsule.rs
index 42ace5c..64157af 100644
--- a/capsules/src/elfloader_capsule.rs
+++ b/capsules/src/elfloader_capsule.rs
@@ -27,7 +27,7 @@
     page_len: u32,
     state: Cell<ElfLoaderState>,
     phantom: PhantomData<&'a ()>,
-    tasks: Cell<[(LoadTasks, bool); 9]>,
+    tasks: Cell<[(LoadTasks, bool); 7]>,
     current_task: OptionalCell<LoadTasks>,
     sel4_state: Cell<SEL4State>,
 }
@@ -36,8 +36,7 @@
 enum LoadTasks {
     FindFile(&'static str /* name */),
     LoadElfHeaders(&'static str /* name */),
-    LoadElf(&'static str /* name */, u32 /* offset */),
-    LoadBinary(&'static str /* name */, u32 /* base */),
+    LoadElf(&'static str /* name */),
     StartSmc,
 }
 
@@ -45,8 +44,6 @@
 struct SEL4State {
     kernel_offset: u32,
     capdl_loader_offset: u32,
-    cantrip_builtins_offset: u32,
-    cantrip_builtins_size: u32,
     kernel_entry_point: u32,
     capdl_loader_entry_point: u32,
     kernel_headers: [Option<Elf32Phdr>; 4],
@@ -62,8 +59,6 @@
         SEL4State {
             kernel_offset: 0,
             capdl_loader_offset: 0,
-            cantrip_builtins_offset: 0,
-            cantrip_builtins_size: 0,
             kernel_entry_point: 0,
             capdl_loader_entry_point: 0,
             kernel_headers: [None, None, None, None],
@@ -76,28 +71,6 @@
     }
 }
 
-// Should I be part of self?
-static LOAD_SEL4_TASKS: [(LoadTasks, bool); 9] = [
-    (LoadTasks::FindFile("kernel"), false),
-    (LoadTasks::FindFile("capdl-loader"), false),
-    (LoadTasks::FindFile("cantrip-builtins"), false),
-    (LoadTasks::LoadElfHeaders("kernel"), false),
-    (LoadTasks::LoadElfHeaders("capdl-loader"), false),
-    (LoadTasks::LoadElf("kernel", 0), false),
-    (
-        LoadTasks::LoadElf(
-            "capdl-loader",
-            0, /* this should get mutated after previous */
-        ),
-        false,
-    ),
-    // Hack: load the builtins bundle into the last .5M of memory; this
-    // can go away when the Mailbox protocol supports fetching files in
-    // the bundle from flash.
-    (LoadTasks::LoadBinary("cantrip-builtins", 0x50380000), false),
-    (LoadTasks::StartSmc, false),
-];
-
 #[derive(Clone, Copy, PartialEq)]
 enum ElfLoaderState {
     Idle,
@@ -118,12 +91,6 @@
         u32,          /* offset_in_page */
         u32,          /* already loaded */
     ),
-    LoadBinary(
-        &'static str, /* name */
-        u32,          /* base */
-        u32,          /* cursor */
-        u32,          /* bytes_read */
-    ),
 }
 
 impl<'a, F: hil::flash::Flash> ElfLoaderCapsule<'a, F> {
@@ -138,7 +105,15 @@
             page_len: 0,
             state: Cell::new(ElfLoaderState::Idle),
             phantom: PhantomData,
-            tasks: Cell::new(LOAD_SEL4_TASKS.clone()),
+            tasks: Cell::new([
+                (LoadTasks::FindFile("kernel"), false),
+                (LoadTasks::FindFile("capdl-loader"), false),
+                (LoadTasks::LoadElfHeaders("kernel"), false),
+                (LoadTasks::LoadElfHeaders("capdl-loader"), false),
+                (LoadTasks::LoadElf("kernel"), false),
+                (LoadTasks::LoadElf("capdl-loader"), false),
+                (LoadTasks::StartSmc, false),
+            ]),
             current_task: OptionalCell::empty(),
             sel4_state: Cell::new(SEL4State::default()),
         }
@@ -182,7 +157,7 @@
 
         self.current_task.map_or_else(
             || {
-                // dprintf!("no current task\r\n");
+                dprintf!("No current task\r\n");
                 loop {}
             },
             |task| match task {
@@ -192,11 +167,11 @@
                 LoadTasks::LoadElfHeaders(file) => {
                     self.load_elf_headers(file);
                 }
-                LoadTasks::LoadElf("kernel", _) => {
+                LoadTasks::LoadElf("kernel") => {
                     dprintf!("Loading seL4 kernel\r\n");
                     self.load_elf("kernel", 0);
                 }
-                LoadTasks::LoadElf("capdl-loader", _) => {
+                LoadTasks::LoadElf("capdl-loader") => {
                     dprintf!("Loading capdl-loader to the page after seL4\r\n");
                     let sel4_pend =
                         matcha_utils::round_up_to_page(matcha_utils::elf_loader::elf_phys_max_opt(
@@ -208,10 +183,6 @@
                         );
                     self.load_elf("capdl-loader", offset);
                 }
-                LoadTasks::LoadBinary(name, base) => {
-                    dprintf!("Loading {} to {:X}\r\n", name, base);
-                    self.load_binary(name, *base);
-                }
                 LoadTasks::StartSmc => {
                     // NB: ui_p_reg_start & co. come from the code in seL4
                     //    the processes these values.
@@ -276,16 +247,17 @@
                         }
                     }
                 }
-                _ => {
-                    panic!("225");
-                }
+                _ => panic!("225"),
             },
         );
     }
 
     pub fn load_sel4(&self) {
-        // May be able to remove this, if we clean up after done.
-        let tasks = LOAD_SEL4_TASKS.clone();
+        // Reset task list to start over.
+        let mut tasks = self.tasks.get();
+        for i in 0..tasks.len() {
+            tasks[i].1 = false;
+        }
         self.tasks.replace(tasks);
         self.run_next_task();
     }
@@ -298,30 +270,14 @@
             "capdl-loader" => self.state.set(ElfLoaderState::LoadElfHeader(
                 self.sel4_state.get().capdl_loader_offset,
             )),
-            _ => {
-                panic!("207");
-            }
+            _ => panic!("207"),
         }
         match self.state.get() {
-            ElfLoaderState::LoadElfHeader(cursor) => {
-                self.read_page(cursor);
-            }
-            _ => {
-                panic!("214");
-            }
+            ElfLoaderState::LoadElfHeader(cursor) => self.read_page(cursor),
+            _ => panic!("214"),
         }
     }
 
-    fn load_binary(&self, name: &'static str, base: u32) {
-        let cursor = match name {
-            "cantrip-builtins" => self.sel4_state.get().cantrip_builtins_offset,
-            _ => panic!("258"),
-        };
-        let new_state = ElfLoaderState::LoadBinary(name, base, cursor, 0);
-        self.state.set(new_state);
-        self.read_page(cursor);
-    }
-
     fn load_elf(&self, name: &'static str, offset: u32) {
         let phdr = match name {
             "kernel" => self.sel4_state.get().kernel_headers[0].unwrap(),
@@ -359,16 +315,16 @@
             );
             match name {
                 "kernel" => {
-                        let mut sel4_state = self.sel4_state.get();
-                        sel4_state.kernel_bss_start = bss_start;
-                        sel4_state.kernel_bss_size = bss_size;
-                        self.sel4_state.replace(sel4_state);
+                    let mut sel4_state = self.sel4_state.get();
+                    sel4_state.kernel_bss_start = bss_start;
+                    sel4_state.kernel_bss_size = bss_size;
+                    self.sel4_state.replace(sel4_state);
                 },
                 "capdl-loader" => {
-                        let mut sel4_state = self.sel4_state.get();
-                        sel4_state.capdl_bss_start = bss_start;
-                        sel4_state.capdl_bss_size = bss_size;
-                        self.sel4_state.replace(sel4_state);
+                    let mut sel4_state = self.sel4_state.get();
+                    sel4_state.capdl_bss_start = bss_start;
+                    sel4_state.capdl_bss_size = bss_size;
+                    self.sel4_state.replace(sel4_state);
                 },
                 _ => panic!("349"),
             };
@@ -404,9 +360,7 @@
                         sel4_state.capdl_loader_entry_point = elf_header.e_entry;
                         self.sel4_state.replace(sel4_state);
                     }
-                    _ => {
-                        panic!("290");
-                    }
+                    _ => panic!("290"),
                 };
             });
             match self.state.get() {
@@ -418,18 +372,13 @@
                         0,
                     ));
                 }
-                _ => {
-                    panic!("245");
-                }
+                _ => panic!("245"),
             }
         });
         match self.state.get() {
-            ElfLoaderState::LoadProgramHeaderTable(cursor, _, _, _) => {
-                self.read_page(cursor);
-            }
-            _ => {
-                panic!("254");
-            }
+            ElfLoaderState::LoadProgramHeaderTable(cursor, ..) =>
+                self.read_page(cursor),
+            _ => panic!("254"),
         }
     }
 
@@ -461,9 +410,7 @@
                                         Some(slot) => {
                                             sel4_state.kernel_headers[slot] = Some(phdr);
                                         }
-                                        None => {
-                                            panic!("380");
-                                        }
+                                        None => panic!("380"),
                                     }
                                     self.sel4_state.replace(sel4_state);
                                 }
@@ -480,22 +427,16 @@
                                         Some(slot) => {
                                             sel4_state.capdl_loader_headers[slot] = Some(phdr);
                                         }
-                                        None => {
-                                            panic!("380");
-                                        }
+                                        None => panic!("380"),
                                     }
                                     self.sel4_state.replace(sel4_state);
                                 }
-                                _ => {
-                                    panic!("317");
-                                }
+                                _ => panic!("317"),
                             };
                         });
                     }
                 }
-                _ => {
-                    panic!("335");
-                }
+                _ => panic!("335"),
             };
         });
         match self.state.get() {
@@ -513,56 +454,7 @@
                     self.run_next_task();
                 }
             }
-            _ => {
-                panic!("375");
-            }
-        }
-    }
-
-    fn load_binary_callback(&self) {
-        match self.state.get() {
-            ElfLoaderState::LoadBinary(name, base, cursor, bytes_read) => {
-                let size = match name {
-                    "cantrip-builtins" => self.sel4_state.get().cantrip_builtins_size,
-                    _ => panic!("463"),
-                };
-                self.read_page.map(|page| {
-                    let mut_page = page.as_mut();
-                    let remaining = size - bytes_read;
-                    let bytes_in_this_page = core::cmp::min(remaining, self.page_len);
-                    smc_ram_memcpy(
-                        &mut mut_page[0..],
-                        base + bytes_read,
-                        bytes_in_this_page as usize,
-                    );
-                });
-
-                let new_state = if (bytes_read + self.page_len) >= size {
-                    ElfLoaderState::Idle
-                } else {
-                    ElfLoaderState::LoadBinary(
-                        name,
-                        base,
-                        cursor + self.page_len,
-                        bytes_read + self.page_len,
-                    )
-                };
-                self.state.set(new_state);
-                match self.state.get() {
-                    ElfLoaderState::Idle => {
-                        self.run_next_task();
-                    }
-                    ElfLoaderState::LoadBinary(_, _, cursor, _) => {
-                        self.read_page(cursor);
-                    }
-                    _ => {
-                        panic!("477");
-                    }
-                };
-            }
-            _ => {
-                panic!("464");
-            }
+            _ => panic!("375"),
         }
     }
 
@@ -600,9 +492,7 @@
                     let segments = match name {
                         "kernel" => self.sel4_state.get().kernel_headers,
                         "capdl-loader" => self.sel4_state.get().capdl_loader_headers,
-                        _ => {
-                            panic!("461");
-                        }
+                        _ => panic!("461"),
                     };
                     let next_index = index + 1;
                     let mut last_segment = false;
@@ -637,9 +527,7 @@
                     }
                 }
             }
-            _ => {
-                panic!("445");
-            }
+            _ => panic!("445"),
         }
     }
 
@@ -650,7 +538,6 @@
             match self.state.get() {
                 ElfLoaderState::FindingFile(name, cursor) => {
                     let found_file = tar_header.name().contains(name);
-                    let size = tar_header.size();
                     self.current_task.map(|task| match task {
                         LoadTasks::FindFile(_file) => {
                             if found_file {
@@ -662,10 +549,6 @@
                                     "capdl-loader" => {
                                         sel4_state.capdl_loader_offset = cursor + self.page_len;
                                     }
-                                    "cantrip-builtins" => {
-                                        sel4_state.cantrip_builtins_offset = cursor + self.page_len;
-                                        sel4_state.cantrip_builtins_size = size;
-                                    }
                                     _ => {}
                                 }
                                 self.sel4_state.replace(sel4_state);
@@ -677,7 +560,7 @@
                                     .set(ElfLoaderState::FindingFile(name, new_cursor));
                             }
                         }
-                        LoadTasks::LoadElf(_file, _) => {
+                        LoadTasks::LoadElf(_file) => {
                             if found_file {
                                 self.state
                                     .set(ElfLoaderState::LoadElfHeader(cursor + self.page_len));
@@ -688,30 +571,18 @@
                                     .set(ElfLoaderState::FindingFile(name, new_cursor));
                             }
                         }
-                        _ => {
-                            panic!("487");
-                        }
+                        _ => panic!("487"),
                     })
                 }
-                _ => {
-                    panic!("491");
-                }
+                _ => panic!("491"),
             };
         });
 
         match self.state.get() {
-            ElfLoaderState::FindingFile(_, cursor) => {
-                self.read_page(cursor);
-            }
-            ElfLoaderState::LoadElfHeader(cursor) => {
-                self.read_page(cursor);
-            }
-            ElfLoaderState::Idle => {
-                self.run_next_task();
-            }
-            _ => {
-                panic!("507");
-            }
+            ElfLoaderState::FindingFile(_, cursor) => self.read_page(cursor),
+            ElfLoaderState::LoadElfHeader(cursor) => self.read_page(cursor),
+            ElfLoaderState::Idle => self.run_next_task(),
+            _ => panic!("507"),
         }
     }
 
@@ -739,6 +610,7 @@
                     if self.fpga {
                         self.load_sel4();
                     } else {
+                        dprintf!("Simulation; bypass loading from SPI flash\r\n");
                         self.mailbox_hal.map(|mb| {
                             matcha_utils::load_sel4(mb);
                         });
@@ -767,24 +639,14 @@
         self.read_page.replace(read_page);
         self.flash_busy.set(false);
         match self.state.get() {
-            ElfLoaderState::FindingFile(_, _) => {
-                self.find_file_callback();
-            }
-            ElfLoaderState::LoadElfHeader(_) => {
-                self.load_elf_header_callback();
-            }
-            ElfLoaderState::LoadProgramHeaderTable(_, _, _, _) => {
-                self.load_program_header_table_callback();
-            }
-            ElfLoaderState::LoadSegmentsNew(_, _, _, _, _, _, _) => {
-                self.load_segment_callback();
-            }
-            ElfLoaderState::LoadBinary(_, _, _, _) => {
-                self.load_binary_callback();
-            }
-            _ => {
-                panic!("583");
-            }
+            ElfLoaderState::FindingFile(..) => self.find_file_callback(),
+            ElfLoaderState::LoadElfHeader(..) =>
+                self.load_elf_header_callback(),
+            ElfLoaderState::LoadProgramHeaderTable(..) =>
+                self.load_program_header_table_callback(),
+            ElfLoaderState::LoadSegmentsNew(..) =>
+                self.load_segment_callback(),
+            _ => panic!("583"),
         }
     }
     fn write_complete(