aboutsummaryrefslogtreecommitdiff
path: root/pipe_image.c
diff options
context:
space:
mode:
Diffstat (limited to 'pipe_image.c')
-rw-r--r--pipe_image.c68
1 files changed, 51 insertions, 17 deletions
diff --git a/pipe_image.c b/pipe_image.c
index ab3568c..01510a5 100644
--- a/pipe_image.c
+++ b/pipe_image.c
@@ -6,18 +6,32 @@
#include "lib/rs232/rs232.h"
#define ANSI_FG_RED "\033[0;31m"
-
-/* This program pipes it's argument file to stdout prepending it */
-/* with it's size (4 bytes, little endian). It is intended to be used */
-/* with our bootloader (i.e. by piping kernel image to UART). */
-
-int main(int argc, char **argv) {
- char *image_file_name = argc > 1 ? argv[1] : "kernel7.img";
+#define ANSI_FG_DEFAULT "\033[0;39m"
+
+/* This program pipes it's argument file to /dev/ttyUSB0 or stdout */
+/* prepending it with it's size (4 bytes, little endian). */
+/* It is intended to be used with our bootloader. */
+
+int main(int argc, const char **argv) {
+ const char *image_file_name = "kernel7.img";
+ _Bool stdout_instead_of_uart = 0;
+
+ if (argc > 1)
+ if (!strcmp(argv[1], "--stdout"))
+ {
+ stdout_instead_of_uart = 1;
+ argc--;
+ argv++;
+ }
+
+ if (argc > 1)
+ image_file_name = argv[1];
FILE *image_file_handle = fopen(image_file_name, "r");
if (!image_file_handle)
- err(-1, "couldn't open" ANSI_FG_RED "%s", image_file_name);
+ err(-1, "couldn't open" ANSI_FG_RED "%s" ANSI_FG_DEFAULT,
+ image_file_name);
if (fseek(image_file_handle, 0, SEEK_END))
err(-1, "error navigating through file");
@@ -33,16 +47,25 @@ int main(int argc, char **argv) {
err(-1, "error navigating through file");
//init comport
- int comport=16;
-
- if(RS232_OpenComport(comport,115200,"8N1",0)==1)
- err(1,"Error opening comport");
+ const int comport=16;
+ if (!stdout_instead_of_uart)
+ if (RS232_OpenComport(comport, 115200, "8N1", 0) == 1)
+ err(-1, "Error opening comport");
uint32_t image_size_le = htole32(image_size);
- if (RS232_SendBuf(comport,(unsigned char*)&image_size_le,4) == 1)
- err(1, "error writing number to serial");
+ if (stdout_instead_of_uart)
+ {
+ if (fwrite((unsigned char*) &image_size_le, 4, 1, stdout) != 1)
+ err(-1, "error writing number to stdout");
+ }
+ else
+ {
+ if (RS232_SendBuf(comport, (unsigned char*) &image_size_le, 4)
+ == 1)
+ err(-1, "error writing number to serial");
+ }
ssize_t bytes_left = image_size;
@@ -54,11 +77,22 @@ int main(int argc, char **argv) {
< 1)
err(-1, "error reading the file");
- if (RS232_SendBuf(comport,buf,bytes_read) == 1)
- err(1, "error writing to serial");
+ if (stdout_instead_of_uart)
+ {
+ if (fwrite((unsigned char*) buf, bytes_read, 1, stdout) != 1)
+ err(-1, "error writing to stdout");
+ }
+ else
+ {
+ if (RS232_SendBuf(comport, buf, bytes_read) == 1)
+ err(-1, "error writing to serial");
+ }
bytes_left -= bytes_read;
}
- RS232_CloseComport(comport);
+ if (!stdout_instead_of_uart)
+ RS232_CloseComport(comport);
+
+ return 0;
}