Added ability to load an arbitrary binary from an ethernet server and launch it
[akaros.git] / user / apps / parlib / run_binary.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <stdint.h>
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <parlib.h>
10
11 extern char * readline(const char *prompt);
12
13 #define READ_SIZE       1024
14 uint8_t* binary_buf;
15
16 void run_binary()
17 {       
18         char * readline_result = readline("\nEnter name of binary to execute: ");
19         if (readline_result == NULL) {
20                 printf("Error reading from console.\n");
21                 return;
22         }
23
24         char * file_name = malloc(strlen(readline_result) + 8);
25         sprintf(file_name, "./test/%s", readline_result);
26         int fd = open(file_name, O_RDONLY, 0);
27         
28         int iters = 1;
29         binary_buf = malloc(READ_SIZE);
30         
31         int total_bytes_read = 0;
32         int bytes_read = read(fd, binary_buf, READ_SIZE);
33         while(bytes_read > 0) {
34                 total_bytes_read += bytes_read; 
35                 binary_buf = realloc(binary_buf, READ_SIZE*(++iters));
36                 bytes_read = read(fd, binary_buf+total_bytes_read, READ_SIZE);
37         }
38         printf("Loading Binary: %s, ROMSIZE: %d\n", readline_result, total_bytes_read);
39         sys_run_binary(binary_buf, NULL, total_bytes_read);
40         free(binary_buf);
41         close(fd);
42 }
43