File: imx_sdp_simulation.c

package info (click to toggle)
imx-usb-loader 0~git20181105.4aa98090-1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 1,312 kB
  • sloc: ansic: 3,933; xml: 417; makefile: 85; python: 29
file content (140 lines) | stat: -rw-r--r-- 3,465 bytes parent folder | download
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
/*
 * imx_sdp_simulation:
 * A simple client side implementation of the Serial Download Protocol (SDP)
 * for testing.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "portable.h"
#include "imx_sdp.h"
#include "image.h"

struct sim_memory *head;
struct sim_memory {
	struct sim_memory *next;
	unsigned int addr;
	unsigned int len;
	unsigned char *buf;
	int offset;
};

int do_simulation(struct sdp_dev *dev, int report, unsigned char *p, unsigned int cnt,
		unsigned int expected, int* last_trans)
{
	static struct sdp_command cur_cmd;
	static struct sim_memory *cur_mem;
	unsigned int offset;
	unsigned mem_addr;

	switch (report) {
	case 1:
		/* Copy command */
		cur_cmd = *((struct sdp_command *)p);
		printf("cmd: %04x\n", cur_cmd.cmd);
		switch (cur_cmd.cmd) {
		case SDP_WRITE_FILE:
		case SDP_WRITE_DCD:
			if (!head) {
				cur_mem = head = malloc(sizeof(*cur_mem));
			} else {
				cur_mem = head;
				while (cur_mem->next)
					cur_mem = cur_mem->next;

				cur_mem->next = malloc(sizeof(*cur_mem));
				cur_mem = cur_mem->next;
			}

			cur_mem->next = NULL;
			cur_mem->addr = BE32(cur_cmd.addr);
			cur_mem->len = BE32(cur_cmd.cnt);
			cur_mem->buf = malloc(cur_mem->len);
			cur_mem->offset = 0;
			break;
		case SDP_READ_REG:
			cur_mem = head;
			while (cur_mem) {
				if (cur_mem->addr <= BE32(cur_cmd.addr) &&
				    cur_mem->addr + cur_mem->len > BE32(cur_cmd.addr)) {
					break;
				}

				cur_mem = cur_mem->next;
			}
			break;
		}
		break;
	case 2:
		/* Data phase, ignore */
		memcpy(cur_mem->buf + cur_mem->offset, p, cnt);
		cur_mem->offset += cnt;
		break;
	case 3:
		/* Simulate security configuration open */
		*((unsigned int *)p) = BE32(0x56787856);
		break;
	case 4:
		/* Return sensible status */
		switch (cur_cmd.cmd) {
		case SDP_WRITE_FILE:
			*((unsigned int *)p) = BE32(0x88888888UL);
			break;
		case SDP_WRITE_DCD:
			*((unsigned int *)p) = BE32(0x128a8a12UL);
			break;
		case SDP_READ_REG:
			cur_mem = head;
			mem_addr = BE32(cur_cmd.addr);
			while (cur_mem) {
				if (cur_mem->addr <=  mem_addr &&
				    cur_mem->addr + cur_mem->len > mem_addr) {
					if ((mem_addr + cnt) > (cur_mem->addr + cur_mem->len))
						return -EIO;
					break;
				}
				cur_mem = cur_mem->next;
			}
			if (!cur_mem)
				return -EIO;
			offset = mem_addr - cur_mem->addr;
			memcpy(p, cur_mem->buf + offset, cnt);
			break;
		case SDP_JUMP_ADDRESS:
			/* A successful jump returns nothing on Report 4 */
			return -7;
		}
		break;
	default:
		break;
	}

	return 0;
}

void do_simulation_cleanup(void)
{
	struct sim_memory *cur_mem = head;

	while (cur_mem) {
		struct sim_memory *free_mem = cur_mem;
		cur_mem = cur_mem->next;
		free(free_mem);
	}
}