-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcommon.h
More file actions
198 lines (171 loc) · 4.97 KB
/
common.h
File metadata and controls
198 lines (171 loc) · 4.97 KB
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
#pragma once
#define _GNU_SOURCE 1
#define _FILE_OFFSET_BITS 64
#undef _WIN32_WINNT
#undef WINVER
#define _WIN32_WINNT 0x0400
#define WINVER _WIN32_WINNT
#define ARGC_MAX 8
#define ARGC_LEN 384
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <ctype.h> // tolower
#include <math.h>
#ifndef LIBUSB_DETACH
/* detach the device from crappy kernel drivers */
#define LIBUSB_DETACH 1
#endif
#if _WIN32
#include <windows.h>
#include <dbt.h>
#include <tchar.h>
#define THRD_MESSAGE_EXIT WM_USER + 1
DWORD WINAPI ThrdFunc(LPVOID lpParam);
#if UNICODE
#define my_strstr wcsstr
#define my_strtol wcstol
#else
#define my_strstr strstr
#define my_strtol strtol
#endif
#else
#include <dirent.h>
#endif
#if USE_LIBUSB
#include <libusb-1.0/libusb.h>
#include <unistd.h>
#else
#include <setupapi.h>
#include "Wrapper.h"
#define fseeko _fseeki64
#define ftello _ftelli64
BOOL FindPort(void);
void usleep(unsigned int us);
#endif
#include "spd_cmd.h"
#define FLAGS_CRC16 1
#define FLAGS_TRANSCODE 2
#define ERR_EXIT(...) \
do { fprintf(stderr, __VA_ARGS__); exit(1); } while (0)
#define DBG_LOG(...) fprintf(stderr, __VA_ARGS__)
#define WRITE16_LE(p, a) do { \
((uint8_t*)(p))[0] = (uint8_t)(a); \
((uint8_t*)(p))[1] = (a) >> 8; \
} while (0)
#define WRITE32_LE(p, a) do { \
((uint8_t*)(p))[0] = (uint8_t)(a); \
((uint8_t*)(p))[1] = (a) >> 8; \
((uint8_t*)(p))[2] = (a) >> 16; \
((uint8_t*)(p))[3] = (a) >> 24; \
} while (0)
#define READ32_LE(p) ( \
((uint8_t*)(p))[0] | \
((uint8_t*)(p))[1] << 8 | \
((uint8_t*)(p))[2] << 16 | \
((uint8_t*)(p))[3] << 24)
#define WRITE16_BE(p, a) do { \
((uint8_t*)(p))[0] = (a) >> 8; \
((uint8_t*)(p))[1] = (uint8_t)(a); \
} while (0)
#define WRITE32_BE(p, a) do { \
((uint8_t*)(p))[0] = (a) >> 24; \
((uint8_t*)(p))[1] = (a) >> 16; \
((uint8_t*)(p))[2] = (a) >> 8; \
((uint8_t*)(p))[3] = (uint8_t)(a); \
} while (0)
#define READ16_BE(p) ( \
((uint8_t*)(p))[0] << 8 | \
((uint8_t*)(p))[1])
#define READ32_BE(p) ( \
((uint8_t*)(p))[0] << 24 | \
((uint8_t*)(p))[1] << 16 | \
((uint8_t*)(p))[2] << 8 | \
((uint8_t*)(p))[3])
typedef struct {
uint8_t* raw_buf, * enc_buf, * recv_buf, * temp_buf;
#if USE_LIBUSB
libusb_device_handle* dev_handle;
int endp_in, endp_out;
#else
ClassHandle* handle;
#endif
#if _WIN32
DWORD iThread;
HANDLE hThread;
#endif
int flags, recv_len, recv_pos;
int raw_len, enc_len, verbose, timeout;
} spdio_t;
typedef struct {
char name[36];
long long size;
} partition_t;
#pragma pack(1)
typedef struct {
uint8_t signature[8];
uint32_t revision;
uint32_t header_size;
uint32_t header_crc32;
int32_t reserved;
uint64_t current_lba;
uint64_t backup_lba;
uint64_t first_usable_lba;
uint64_t last_usable_lba;
uint8_t disk_guid[16];
uint64_t partition_entry_lba;
int32_t number_of_partition_entries;
uint32_t size_of_partition_entry;
uint32_t partition_entry_array_crc32;
} efi_header;
typedef struct {
uint8_t partition_type_guid[16];
uint8_t unique_partition_guid[16];
uint64_t starting_lba;
uint64_t ending_lba;
int64_t attributes;
uint8_t partition_name[72];
} efi_entry;
typedef struct {
uint32_t dwVersion;
uint32_t bDisableHDLC; //0: Enable hdl; 1:Disable hdl
uint8_t bIsOldMemory;
uint8_t bSupportRawData;
uint8_t bReserve[2];
uint32_t dwFlushSize; //unit KB
uint32_t dwStorageType;
uint32_t dwReserve[59]; //Reserve
}DA_INFO_T;
#pragma pack()
void print_string(FILE* f, const void* src, size_t n);
#if USE_LIBUSB
void find_endpoints(libusb_device_handle* dev_handle, int result[2]);
#else
BOOL ChangeMode(spdio_t* io);
#endif
spdio_t* spdio_init(int flags);
void spdio_free(spdio_t* io);
void encode_msg(spdio_t* io, int type, const void* data, size_t len);
int send_msg(spdio_t* io);
int recv_msg(spdio_t* io);
int recv_msg_timeout(spdio_t* io, int timeout);
unsigned recv_type(spdio_t* io);
int send_and_check(spdio_t* io);
void check_confirm(const char* name);
void send_file(spdio_t* io, const char* fn, uint32_t start_addr, int end_data, unsigned step);
unsigned dump_flash(spdio_t* io, uint32_t addr, uint32_t start, uint32_t len, const char* fn, unsigned step);
unsigned dump_mem(spdio_t* io, uint32_t start, uint32_t len, const char* fn, unsigned step);
uint64_t dump_partition(spdio_t* io, const char* name, uint64_t start, uint64_t len, const char* fn, unsigned step);
void dump_partitions(spdio_t* io, const char* fn, int* nand_info, int blk_size);
uint64_t read_pactime(spdio_t* io);
partition_t* partition_list(spdio_t* io, const char* fn, int* part_count_ptr);
void repartition(spdio_t* io, const char* fn);
void erase_partition(spdio_t* io, const char* name);
void load_partition(spdio_t* io, const char* name, const char* fn, unsigned step);
void load_nv_partition(spdio_t* io, const char* name, const char* fn, unsigned step);
void load_partitions(spdio_t* io, const char* path, int blk_size);
uint64_t find_partition_size(spdio_t* io, const char* name);
uint64_t str_to_size(const char* str);
uint64_t str_to_size_ubi(const char* str, int* nand_info);
void get_Da_Info(spdio_t* io);