summaryrefslogtreecommitdiff
path: root/upload/upload.c
blob: 2ed07e0579b87cc51d1f9320588918957ba08041 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include "legato.h"
#include "interfaces.h"
#include "xmodem.h"
#include "util.h"
#include <sys/mman.h>

#define MTK7697_BAUD 115200
#define MTK7697_BAUDX 115200 * 8
static const char* SERIAL_PORT_PATH = "/dev/ttyHS0";
static const char* LDR_BIN_PATH = "/home/root/mtfiles/mt7697_bootloader.bin";
static const char* N9_BIN_PATH =
    "/home/root/mtfiles/WIFI_RAM_CODE_MT76X7_in_flash.bin";
static const char* CM4_BIN_PATH = "/home/root/mtfiles/ble_smart_connect.bin";
static const char* DA_PATH = "/home/root/mtfiles/da97.bin";

typedef enum { LDR = 1, N9 = 3, CM4 = 2 } MtkMemorySegment;

typedef struct {
  int serialPort;
  int errorCount;
  int cCount;
  int retry;
  int startTime;
} FlashState;

FlashState state = {0, 0, 0, 0};

uint8_t _inbyte(unsigned short timeout) {
  uint8_t x;
  if (read(state.serialPort, &x, 1) != 1)
    return -1;

  LE_INFO("in: %c", x);
  return x;
}

void _outbyte(uint8_t c) {
  LE_INFO("out: %c", c);
  write(state.serialPort, &c, 1);
}

int xmodemSendFile(const char* filename) {
  int fd = open(filename, O_RDONLY);
  struct stat buf;
  if (fstat(fd, &buf) != 0) {
    return -1;
  }
  unsigned char* file = mmap(NULL, buf.st_size, PROT_READ, MAP_SHARED, fd, 0);
  int r = xmodemTransmit(file, buf.st_size);
  close(fd);
  return r;
}

void configureSerialPort(FlashState* s, int baud) {
  close(s->serialPort);
  s->serialPort = fd_openSerial(SERIAL_PORT_PATH, baud);
}

bool flashDa(FlashState* s) {
  configureSerialPort(&state, MTK7697_BAUD);
  LE_INFO("Sending DA over xmodem");
  int r = xmodemSendFile(DA_PATH);
  LE_INFO("DA bytes sent: %d", r);
  return r;
}

void putIntoBootloader() {
  // make sure the bootstrap pin is on
  mtBootstrap_Activate();
  // reset state && wait
  mtRst_Deactivate();
  sleep(1);
  // pull out of reset
  mtRst_Activate();
}

void retryInitSequence(FlashState* s) {
  s->retry++;
  s->cCount = 0;
  s->errorCount = 0;
  s->startTime = util_getUnixDatetime();
  putIntoBootloader();
  configureSerialPort(s, MTK7697_BAUD);
}

bool verifyInitSequence(FlashState* s) {
  uint8_t data;
  bool initDone = false;
  s->startTime = util_getUnixDatetime();
  while (!initDone) {
    data = fd_getChar(s->serialPort);
    fd_flush(s->serialPort);
    if (data == 'C') {
      s->cCount++;
      LE_INFO("Got a C");
    } else if (data != 0) {
      s->errorCount++;
      LE_INFO("Got an error");
    }
    if (s->cCount > 1) {
      initDone = true;
      LE_INFO("Init done");
      break;
    }
    if (s->errorCount > 3 || (util_getUnixDatetime() - s->startTime > 3)) {
      LE_INFO("Retrying...");
      retryInitSequence(s);
    }
    if (s->retry > 3) {
      LE_INFO("Aborting");
      break;
    }
  }
  return initDone;
}

bool flashBinary(FlashState* s, MtkMemorySegment segment, const char* binPath) {
  putIntoBootloader();
  configureSerialPort(s, MTK7697_BAUD);
  bool init = verifyInitSequence(s);
  LE_INFO("Init verified: %d", init);
  flashDa(s);
  configureSerialPort(s, MTK7697_BAUDX);
  sleep(1);
  bool initDone = false;
  int data = '0';
  switch (segment) {
    case LDR:
      fd_puts(s->serialPort, "1\r");
      break;
    case N9:
      fd_puts(s->serialPort, "3\r");
      break;
    case CM4:
      fd_puts(s->serialPort, "2\r");
      break;
  }
  while (!initDone) {
    LE_INFO("here %c", data);
    data = fd_getChar(s->serialPort);
    if (data == 'C') {
      initDone = true;
      break;
    }
  }
  fd_flush(s->serialPort);
  LE_INFO("Sending %s over xmodem", binPath);
  // this returns how much data was sent,
  // so for now check that it's over 0
  bool r = xmodemSendFile(binPath) > 0;
  LE_INFO("%s success: %d", binPath, r);
  sleep(1);
  fd_puts(s->serialPort, "C\r");
  fd_flush(s->serialPort);
  return true;
}

bool flashLdr(FlashState* s) {
  LE_INFO("Flashing LDR segment");
  return flashBinary(s, LDR, LDR_BIN_PATH);
}

bool flashN9(FlashState* s) {
  LE_INFO("Flashing N9 segment");
  return flashBinary(s, N9, N9_BIN_PATH);
}

bool flashCm4(FlashState* s) {
  LE_INFO("Flashing CM4");
  return flashBinary(s, CM4, CM4_BIN_PATH);
}

void resetPins() {
  mtBootstrap_Deactivate();
}

void configureGpio() {
  mtRst_SetPushPullOutput(MTRST_ACTIVE_HIGH, true);
  mtBootstrap_SetPushPullOutput(MTBOOTSTRAP_ACTIVE_HIGH, true);
}

COMPONENT_INIT {
  configureGpio();
  flashLdr(&state);
  flashN9(&state);
  flashCm4(&state);
  resetPins();
  close(state.serialPort);
}