summaryrefslogtreecommitdiff
path: root/upload/upload.c
blob: 90ea2810b4bb5fc3fc42c04714e43d269519d24c (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
#include "legato.h"
#include "interfaces.h"
#include "xmodem.h"
#include "util.h"
#include <sys/mman.h>

#define MTK7697_BAUD 115200
#define MTK7697_BAUDX 115200 * 2
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 = {-1, 0, 0, 0};

bool xmodemSendFile(const char* filename) {
  return !xymodem_send(state.serialPort, filename, PROTOCOL_XMODEM, false);
}

void configureSerialPort(FlashState* s, int baud) {
  if (s->serialPort != -1) {
    close(s->serialPort);
  }
  s->serialPort = open_serial(SERIAL_PORT_PATH, fd_convertBaud(baud));
}

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

int putAndIntercept(int fd, const char* s) {
  return writeAndIntercept(fd, s, strlen(s));
}

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 mtk_verifyInitSequence(FlashState* s) {
  uint8_t data;
  bool initDone = false;
  s->startTime = util_getUnixDatetime();
  while (!initDone) {
    data = fd_getChar(s->serialPort);
    fd_flushInput(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 = mtk_verifyInitSequence(s);
  LE_INFO("Init verified: %d", init);
  flashDa(s);
  configureSerialPort(s, MTK7697_BAUDX);
  sleep(1);
  int bytesWritten = 0;
  const char* memorySelection;
  switch (segment) {
    case LDR:
      memorySelection = "1\r";
      break;
    case CM4:
      memorySelection = "2\r";
      break;
    case N9:
      memorySelection = "3\r";
      break;
  }
  bytesWritten = putAndIntercept(s->serialPort, memorySelection);
  LE_INFO("Bytes written %d", bytesWritten);
  fd_flush(s->serialPort);
  fd_flushInput(s->serialPort);
  bool initDone = false;
  int data = '0';
  while (!initDone) {
    LE_INFO("here %c", data);
    data = fd_getChar(s->serialPort);
    if (data == 'C') {
      initDone = true;
      break;
    }
  }
  fd_flush(s->serialPort);
  fd_flushInput(s->serialPort);
  LE_INFO("Sending %s over xmodem", binPath);
  putAndIntercept(s->serialPort, "\r\r\r");
  bool r = xmodemSendFile(binPath);
  LE_INFO("%s success: %d", binPath, r);
  sleep(1);
  putAndIntercept(s->serialPort, "C\r");
  fd_flush(s->serialPort);
  fd_flushInput(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 segment");
  return flashBinary(s, CM4, CM4_BIN_PATH);
}

void resetPins() {
  mtBootstrap_Deactivate();
}

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

void mtk_init() {
  char path[1024];
  snprintf(path, 1024, "/home/root/outputdata%d", util_getUnixDatetime());
  outputFd = open(path, O_RDWR | O_CREAT);
  LE_INFO("Output fd %d", outputFd);
  mtk_configureGpio();
  flashLdr(&state);
  flashN9(&state);
  flashCm4(&state);
  resetPins();
  close(state.serialPort);
  close(outputFd);
}