aboutsummaryrefslogtreecommitdiffstats
path: root/firmware/zpu/usrp2p/bootloader_utils.c
blob: 371c518a76a49524a721416bc3847ddcf7eea65a (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
/* -*- c++ -*- */
/*
 * Copyright 2010 Ettus Research LLC
 *
 */

//contains routines for loading programs from Flash. depends on Flash libraries.
//also contains routines for reading / writing EEPROM flags for the bootloader
#include <stdbool.h>
#include <string.h>
#include <bootloader_utils.h>
#include <spi_flash.h>
#include <memory_map.h>
#include <nonstdio.h>

int is_valid_fpga_image(uint32_t addr) {
//	printf("is_valid_fpga_image(): starting with addr=%x...\n", addr);
	uint8_t imgbuf[64];
	spi_flash_read(addr, 64, imgbuf);
	//we're just looking for leading 0xFF padding, followed by the sync bytes 0xAA 0x99
	for(size_t i = 0; i<63; i++) {
		if(imgbuf[i] == 0xFF) continue;
		if(imgbuf[i] == 0xAA && imgbuf[i+1] == 0x99) {
			//printf("is_valid_fpga_image(): found valid FPGA image\n");
			return 1;
		}
	}
	
	return 0;
}

int is_valid_fw_image(uint32_t addr) {
	static const uint8_t fwheader[] = {0x0b, 0x0b, 0x0b, 0x0b}; //just lookin for a jump to anywhere located at the reset vector
	//printf("is_valid_fw_image(): starting with addr=%x...\n", addr);
	uint8_t buf[12];
	spi_flash_read(addr, 4, buf);
	//printf("is_valid_fw_image(): read ");
	//for(int i = 0; i < 5; i++) printf("%x ", buf[i]);
	//printf("\n");
	return memcmp(buf, fwheader, 4) == 0;
}

void start_program(void)
{
	//ignoring the addr now
	//all this does is tap that register
	*((volatile uint32_t *) SR_ADDR_BLDRDONE) = 1;
}