aboutsummaryrefslogtreecommitdiffstats
path: root/firmware/microblaze/lib/loader_parser.c
blob: 96457a164f3a66b6b2091cd3ecbea5fcc7c85d2c (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/* -*- c++ -*- */
/*
 * Copyright 2009 Ettus Research LLC
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "loader_parser.h"
#include <quadradio/loader_bits.h>
#include <quadradio/flashdir.h>
#include <quadradio/simple_binary_format.h>
#include <spi_flash.h>
#include <nonstdio.h>
//#include <assert.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "ethernet.h"
#include "qr_settings.h"

#define min(a,b) ((a) < (b) ? (a) : (b))

static spi_flash_async_state_t async_state;


static caldiv_eeprom_setter_t _caldiv_set_rev = NULL;
static caldiv_eeprom_setter_t _caldiv_set_ser = NULL;
static caldiv_eeprom_setter_t _caldiv_set_mod = NULL;

void
register_caldiv_eeprom_setters(caldiv_eeprom_setter_t set_rev,
			       caldiv_eeprom_setter_t set_ser,
			       caldiv_eeprom_setter_t set_mod)
{
  _caldiv_set_rev = set_rev;
  _caldiv_set_ser = set_ser;
  _caldiv_set_mod = set_mod;
}


// big-endian
static uint32_t 
get32(const unsigned char *s)
{
  return (s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3];
}

// big-endian
static unsigned char *
put32(unsigned char *s, uint32_t v)
{
  s[0] = (v >> 24) & 0xff;
  s[1] = (v >> 16) & 0xff;
  s[2] = (v >>  8) & 0xff;
  s[3] = v & 0xff;
  return s + 4;
}

static bool
erased_p(uint32_t flash_addr, size_t nbytes)
{
  unsigned char	buf[64];

  size_t n;
  for (size_t i = 0; i < nbytes; i += n, flash_addr += n){
    n = min(nbytes - i, sizeof(buf));
    spi_flash_read(flash_addr, n, buf);
    for (size_t j = 0; j < n; j++)
      if (buf[j] != 0xff)
	return false;
  }
  return true;
}

static bool
erase_flash(uint32_t addr, uint32_t len)
{
  if (addr % spi_flash_sector_size() != 0)
    return false;

  if (len % spi_flash_sector_size() != 0)
    return false;

  spi_flash_async_erase_start(&async_state, addr, len);
  // FIXME? check to see if erase was successful
  return true;
}

static bool
map_slot(uint32_t slot, uint32_t *slot_start, uint32_t *slot_len, uint32_t *status)
{
  // This case doesn't require a valid flashdir, and in fact can be used as 
  // part of writing the intial flashdir.
  if (QLD_SLOT_DOM(slot) == QLD_DOM_UNMAPPED){
    int flash_size = get_flash_size();
    if (flash_size == 0){
      *status = QLDS_FAILED;	// Can't find the flash.  most likely a h/w problem.
      return false;
    }
    *slot_start = 0;
    *slot_len = flash_size;
    return true;
  }

  const struct flashdir *fd = get_flashdir();
  if (fd == 0)
    return false;

  uint32_t slot_num = QLD_SLOT_NUM(slot);

  switch(QLD_SLOT_DOM(slot)){
  case QLD_DOM_FPGA:
    if (slot_num >= fd->fpga_nslots){
      *status = QLDS_INVALID_ARG;
      return false;
    }
    *slot_start = fd->slot[slot_num + fd->fpga_slot0].start << spi_flash_log2_sector_size();
    *slot_len = fd->slot[slot_num + fd->fpga_slot0].len << spi_flash_log2_sector_size();
    return true;

  case QLD_DOM_FW:
    if (slot_num >= fd->fw_nslots){
      *status = QLDS_INVALID_ARG;
      return false;
    }
    *slot_start = fd->slot[slot_num + fd->fw_slot0].start << spi_flash_log2_sector_size();
    *slot_len = fd->slot[slot_num + fd->fw_slot0].len << spi_flash_log2_sector_size();
    return true;

  default:
    *status = QLDS_INVALID_ARG;
    return false;
  }
}


static bool
check_flashdir(void)
{
  return get_flashdir() != 0;
}

void
loader_parser(const unsigned char *input, size_t ilen,
	      unsigned char *output, size_t max_olen, size_t *actual_olen)
{
  //assert (max_olen >= 8);
  if (!(max_olen >= 8))
    abort();

  *actual_olen = 0;
  uint32_t status = QLDS_BAD_PKT;

  uint32_t cmd = get32(input);
  uint32_t nonce = get32(input+4);
  uint32_t slot = 0;
  uint32_t addr = 0;
  uint32_t len = 0;

  if (ilen < 8){
    nonce = -1;
    goto done;
  }

  uint32_t slot_start;		// offset in flash
  uint32_t slot_len;		// length in bytes
  
  if (ilen >= 5 * sizeof(uint32_t)){
    slot = get32(input+8);
    addr = get32(input+12);
    len = get32(input+16);
  }

  switch (cmd){
  case QLD_FLASH_ERASE_START:
    // <QLD_FLASH_ERASE_START> <nonce> <slot> <addr> <len>
    if (ilen != 5 * sizeof(uint32_t))
      goto done;

    if (!check_flashdir()){
      status = QLDS_BAD_FLASHDIR;
      goto done;
    }
    if (!map_slot(slot, &slot_start, &slot_len, &status))
      goto done;

    if (QLD_SLOT_DOM(slot) != QLD_DOM_UNMAPPED){
      addr = slot_start;
      len = slot_len;
    }
    //printf("flash_erase: addr = 0x%x, len=0x%x\n", addr, len);

    if (0 && erased_p(addr, len)){	// already erased?
      async_state.first = async_state.last = async_state.current = 0;
      goto ok;
    }

    if (erase_flash(addr, len))
      goto ok;

    status = QLDS_FAILED;
    goto done;
    

  case QLD_FLASH_ERASE_POLL:
    // <QLD_FLASH_ERASE_POLL> <nonce>
    if (ilen != 2 * sizeof(uint32_t))
      goto done;

    if (spi_flash_async_erase_poll(&async_state))
      goto ok;

    status = QLDS_BUSY;
    goto done;


  case QLD_FLASH_WRITE:
    // <QLD_FLASH_WRITE> <nonce> <slot> <addr> <len> <data ...>
    if (ilen < 5 * sizeof(uint32_t))
      goto done;

    if (ilen != 5 * sizeof(uint32_t) + len)
      goto done;

    if (!check_flashdir()){
      status = QLDS_BAD_FLASHDIR;
      goto done;
    }
    if (!map_slot(slot, &slot_start, &slot_len, &status))
      goto done;

    addr += slot_start;
    len = min(len, slot_len);

    if (spi_flash_program(addr, len, &input[5*sizeof(uint32_t)]))
      goto ok;

    status = QLDS_FAILED;
    goto done;
    

  case QLD_FLASH_READ:
  case QLD_MEM_READ:
  case QLD_MEM_WRITE:
  case QLD_GOTO:
    status = QLDS_NOTIMPLEMENTED;
    goto done;

  case QLD_PING:
    // <QLD_PING> <nonce>
    if (ilen != 2 * sizeof(uint32_t))
      goto done;
    goto ok;

#if 0
  case QLD_EEPROM_SET_XXX:
  // <QLD_EEPROM_SET_XXX> <nonce> <arg> <idlen> <idstr> <data ...>
  {
    uint32_t arg    = get32(input+2*sizeof(uint32_t));
    uint32_t idlen  = get32(input+3*sizeof(uint32_t));
    uint8_t *idstr  = (uint8_t*)input+4*sizeof(uint32_t);
    uint8_t *data_p = idstr+idlen;
    
    //handle the ethernet cases
    if (strncmp((char*)idstr, "ip", idlen) == 0){
        struct ip_addr addr = {get32(data_p)};
        ethernet_set_ip_addr(arg, addr);
    }
    else if (strncmp((char*)idstr, "mac", idlen) == 0){
        eth_mac_addr_t addr;
        memcpy(&addr, data_p, sizeof(addr));
        ethernet_set_mac_addr(arg, &addr);
    }
    //handle the main board eeprom
    else if (strncmp((char*)idstr, "qrrev", idlen) == 0){
        qr_set_revision(get32(data_p));
    }
    else if (strncmp((char*)idstr, "qrser", idlen) == 0){
        qr_set_serial(get32(data_p));
    }
    else if (strncmp((char*)idstr, "qrmod", idlen) == 0){
        qr_set_model(get32(data_p));
    }
    //handle the caldiv eeprom
    else if (strncmp((char*)idstr, "cdrev", idlen) == 0){
        if (_caldiv_set_rev) _caldiv_set_rev(get32(data_p));
    }
    else if (strncmp((char*)idstr, "cdser", idlen) == 0){
        if (_caldiv_set_ser) _caldiv_set_ser(get32(data_p));
    }
    else if (strncmp((char*)idstr, "cdmod", idlen) == 0){
        if (_caldiv_set_ser) _caldiv_set_mod(get32(data_p));
    }
    else {
        goto done;
    }
  }
  goto ok;
#endif

  default:
    status = QLDS_UNKNOWN_CMD;
    goto done;
  }

 ok:
  status = QLDS_OK;

 done:
  put32(output, nonce);
  put32(output+4, status);
  *actual_olen = 2*sizeof(uint32_t);
}