|
@@ -157,6 +157,37 @@ const char *sources[][2] = {
|
|
|
#include "ipxe_handover.h"
|
|
|
#include "asmc_api.h"
|
|
|
|
|
|
+#include "sha-2/sha-256.c"
|
|
|
+
|
|
|
+void *read_whole_file(FILE *fin, size_t *size) {
|
|
|
+ size_t buf_size = 16;
|
|
|
+ char *buf = malloc(buf_size);
|
|
|
+ size_t buf_pos = 0;
|
|
|
+ while (1) {
|
|
|
+ size_t res = fread(buf + buf_pos, 1, buf_size - buf_pos, fin);
|
|
|
+ buf_pos += res;
|
|
|
+ if (buf_pos < buf_size) {
|
|
|
+ break;
|
|
|
+ } else {
|
|
|
+ buf_size *= 2;
|
|
|
+ buf = realloc(buf, buf_size);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (size) {
|
|
|
+ *size = buf_pos;
|
|
|
+ }
|
|
|
+ return buf;
|
|
|
+}
|
|
|
+
|
|
|
+void compute_file_sha256(const char *filename, uint8_t hash[32]) {
|
|
|
+ FILE *fin = fopen(filename, "rb");
|
|
|
+ size_t size;
|
|
|
+ void *buf = read_whole_file(fin, &size);
|
|
|
+ fclose(fin);
|
|
|
+ calc_sha_256(hash, buf, size);
|
|
|
+ free(buf);
|
|
|
+}
|
|
|
+
|
|
|
int table_comp(void *t1, void *t2) {
|
|
|
table_sect *s1 = t1;
|
|
|
table_sect *s2 = t2;
|
|
@@ -260,18 +291,36 @@ void *pop_from_ipxe() {
|
|
|
|
|
|
coro_t *coro_ipxe;
|
|
|
|
|
|
-downloaded_file *download_file(const char *url, const char *dest_file) {
|
|
|
+void download_file(const char *url, const char *dest_file) {
|
|
|
push_to_ipxe(strdup("download"));
|
|
|
push_to_ipxe(strdup(url));
|
|
|
coro_enter(coro_ipxe);
|
|
|
downloaded_file *df = pop_from_ipxe();
|
|
|
assert(df->size > 0);
|
|
|
- /*printf("Received document of %d bytes!\n", df->size);
|
|
|
- printf("---\n");
|
|
|
- for (size_t i = 0; i < df->size; i++) {
|
|
|
- putchar(df->data[i]);
|
|
|
+ FILE *fout = fopen(dest_file, "wb");
|
|
|
+ fwrite(df->data, 1, df->size, fout);
|
|
|
+ fclose(fout);
|
|
|
+ free(df->data);
|
|
|
+ free(df);
|
|
|
+}
|
|
|
+
|
|
|
+void download_check_file(const char *url, const char *dest_file, const uint8_t hash[32]) {
|
|
|
+ push_to_ipxe(strdup("download"));
|
|
|
+ push_to_ipxe(strdup(url));
|
|
|
+ coro_enter(coro_ipxe);
|
|
|
+ downloaded_file *df = pop_from_ipxe();
|
|
|
+ assert(df->size > 0);
|
|
|
+ uint8_t computed_hash[32];
|
|
|
+ calc_sha_256(computed_hash, df->data, df->size);
|
|
|
+ printf("Computed hash:");
|
|
|
+ size_t i;
|
|
|
+ for (i = 0; i < 32; i++) {
|
|
|
+ printf(" %02x", computed_hash[i]);
|
|
|
+ }
|
|
|
+ printf("\n");
|
|
|
+ for (i = 0; i < 32; i++) {
|
|
|
+ assert(computed_hash[i] == hash[i]);
|
|
|
}
|
|
|
- printf("---\n");*/
|
|
|
FILE *fout = fopen(dest_file, "wb");
|
|
|
fwrite(df->data, 1, df->size, fout);
|
|
|
fclose(fout);
|
|
@@ -307,29 +356,32 @@ void *compile_file(const char *filename, const char *symbol, void **buffer) {
|
|
|
tcc_add_symbol(state, "tcc_get_symbol", tcc_get_symbol);
|
|
|
tcc_add_symbol(state, "download_file", download_file);
|
|
|
tcc_add_symbol(state, "compile_file", compile_file);
|
|
|
+ tcc_add_symbol(state, "calc_sha_256", calc_sha_256);
|
|
|
+ tcc_add_symbol(state, "read_whole_file", read_whole_file);
|
|
|
+ tcc_add_symbol(state, "compute_file_sha256", compute_file_sha256);
|
|
|
int res;
|
|
|
for (int i = 0; i < sizeof(includes2) / sizeof(includes2[0]); i++) {
|
|
|
res = tcc_add_include_path(state, includes2[i]);
|
|
|
if (res) {
|
|
|
printf("tcc_add_include_path() failed...\n");
|
|
|
- return 1;
|
|
|
+ return 0;
|
|
|
}
|
|
|
}
|
|
|
res = tcc_add_file(state, filename);
|
|
|
if (res) {
|
|
|
printf("tcc_add_file() failed...\n");
|
|
|
- return 1;
|
|
|
+ return 0;
|
|
|
}
|
|
|
int size = tcc_relocate(state, NULL);
|
|
|
if (size < 0) {
|
|
|
printf("tcc_relocate() failed...\n");
|
|
|
- return 1;
|
|
|
+ return 0;
|
|
|
}
|
|
|
*buffer = malloc(size);
|
|
|
size = tcc_relocate(state, *buffer);
|
|
|
if (size < 0) {
|
|
|
printf("second tcc_relocate() failed...\n");
|
|
|
- return 1;
|
|
|
+ return 0;
|
|
|
}
|
|
|
void *symb = tcc_get_symbol(state, symbol);
|
|
|
tcc_delete(state);
|
|
@@ -416,10 +468,17 @@ int main(int argc, char *argv[]) {
|
|
|
|
|
|
// Download the continuation program
|
|
|
const char *url = "http://10.0.2.2:8080/continue.c";
|
|
|
- printf("Request to download %s\n", url);
|
|
|
+ printf("Request to download %s...\n", url);
|
|
|
+ const uint32_t hash[32] = {0xEB, 0x42, 0xD9, 0x44, 0xC2, 0x2E, 0x46, 0x4F, 0x39, 0xAF, 0x75, 0x31, 0xED, 0x74, 0x99, 0xA1,
|
|
|
+ 0x1D, 0xC5, 0xDC, 0x6E, 0x34, 0x77, 0x3C, 0x35, 0xFE, 0x14, 0x1B, 0xAC, 0x5D, 0x53, 0x3D, 0x5D};
|
|
|
download_file(url, "/ram/continue.c");
|
|
|
+ //download_check_file(url, "/ram/continue.c", hash);
|
|
|
void *cont_buf;
|
|
|
int (*cont_symb)(int, char*[]) = compile_file("/ram/continue.c", "_start", &cont_buf);
|
|
|
+ if (!cont_symb) {
|
|
|
+ printf("Compilation failed...\n");
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
char *cont_argv[] = {"_main"};
|
|
|
cont_symb(sizeof(cont_argv) / sizeof(cont_argv[0]), cont_argv);
|
|
|
free(cont_buf);
|
|
@@ -430,7 +489,7 @@ int main(int argc, char *argv[]) {
|
|
|
|
|
|
//res = main_symb(&ih);
|
|
|
|
|
|
- printf("Returning from iPXE!\n");
|
|
|
+ printf("iPXE exited!\n");
|
|
|
coro_destroy(coro_ipxe);
|
|
|
free_handover(&ih);
|
|
|
free(ipxe_buf);
|