aboutsummaryrefslogtreecommitdiff
path: root/src/host/pipe_image.c
blob: 105f9a1f40677c4e48127e653b8ca59100fc5679 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <stdio.h>
#include <err.h>
#include <endian.h>
#include <stdint.h>
#include <sys/types.h>
#include "rs232.h"

#define ANSI_FG_RED "\033[0;31m"
#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 = "kernel.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" ANSI_FG_DEFAULT,
	image_file_name);

  if (fseek(image_file_handle, 0, SEEK_END))
    err(-1, "error navigating through file");

  ssize_t image_size = ftell(image_file_handle);
  if (image_size < 0)
    err(-1, "couldn't get image file size");

  if (image_size >> 32)
    err(-1, "file to big (should be smaller than 4G)");

  if (fseek(image_file_handle, 0, SEEK_SET))
    err(-1, "error navigating through file");

  //init comport
  const int comport=16;

  if (!stdout_instead_of_uart)
    if (RS232_OpenComport(comport, 115200, "8N1", 1) == 1)
      err(-1, "Error opening comport");

  uint32_t image_size_le = htole32(image_size);

  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;

  unsigned char buf[1024];
  while (bytes_left)
    {
      size_t bytes_read;
      if ((bytes_read = fread(buf, 1, sizeof(buf), image_file_handle))
	  < 1)
	err(-1, "error reading the file");

      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;
    }
/*
  while(1){
      int bytes_read=read(0,buf,sizeof(buf));
      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");
	}
  }
  */
  if (!stdout_instead_of_uart)
    RS232_CloseComport(comport);

  return 0;
}