1616// limitations under the License.
1717
1818#include < os>
19+ #include < util/elf_binary.hpp>
20+ #include < util/sha1.hpp>
21+
22+ bool verb = true ;
23+
24+ #define MYINFO (X,...) INFO(" Service" , X, ##__VA_ARGS__)
25+
26+ extern " C" void hotswap (const char * base, int len, char * dest, void * start);
1927
2028void Service::start (const std::string& args)
2129{
22- printf (" Testing kernel modules. Args: %s \n " , args.c_str ());
30+ MYINFO (" Testing kernel modules. Args: %s" , args.c_str ());
2331
2432 auto mods = OS::modules ();
2533
26- Expects (mods.size () == 3 );
34+ // Expects(mods.size() == 3);
2735
2836 printf (" Found %i modules: \n " , mods.size ());
2937
3038 for (auto mod : mods)
31- printf ( " \t * %s @ 0x%x - 0x%x, size: %ib \n " ,
39+ INFO2 ( " * %s @ 0x%x - 0x%x, size: %ib" ,
3240 reinterpret_cast <char *>(mod.cmdline ),
33- mod.mod_start , mod.mod_end , mod.mod_end - mod.mod_start );
41+ mod.mod_start , mod.mod_end , mod.mod_end - mod.mod_start );
3442
3543 // Verify module cmdlines
3644 Expects (std::string ((char *)mods[0 ].cmdline ) == " ../mod1.json" );
@@ -44,11 +52,34 @@ void Service::start(const std::string& args)
4452 Expects (std::string ((char *)mods[2 ].mod_start )
4553 == " {\" module3\" : \" More JSON data, for mod2 service\" }\n " );
4654
47- // TODO: Properly verify mod2 as ELF binary
48- Expects (((uint8_t *) mods[1 ].mod_start )[0 ] == 0x7f );
49- Expects (((char *) mods[1 ].mod_start )[1 ] == ' E' );
50- Expects (((char *) mods[1 ].mod_start )[2 ] == ' L' );
51- Expects (((char *) mods[1 ].mod_start )[3 ] == ' F' );
55+ multiboot_module_t binary = mods[1 ];
56+
57+ MYINFO (" Verifying mod2 as ELF binary" );
58+ Elf_binary elf ({(char *)binary.mod_start , (int )(binary.mod_end - binary.mod_start )});
59+
60+ MYINFO (" Moving hotswap function (now at %p)" , &hotswap);
61+ memcpy ((void *)0x8000 , (void *)&hotswap, 1024 );
62+
63+ MYINFO (" Preparing for jump to %s" , (char *)binary.cmdline );
64+
65+ char * base = (char *)binary.mod_start ;
66+ int len = (int )(binary.mod_end - binary.mod_start );
67+ char * dest = (char *)0x100000 ;
68+ void * start = (void *)elf.entry ();
69+
70+ SHA1 sha;
71+ sha.update (base, len);
72+ MYINFO (" Sha1 of ELF binary module: %s" , sha.as_hex ().c_str ());
73+
74+
75+ MYINFO (" Jump params: base: %p, len: %i, dest: %p, start: %p" ,
76+ base, len, dest, start);
77+
78+ MYINFO (" Disabling interrupts and calling hotswap..." );
79+
80+ asm (" cli" );
81+ ((decltype (&hotswap))0x8000 )(base, len, dest, start);
82+
83+ panic (" Should have jumped\n " );
5284
53- exit (0 );
5485}
0 commit comments