-
Notifications
You must be signed in to change notification settings - Fork 2
/
mphidflash.h
143 lines (118 loc) · 4.38 KB
/
mphidflash.h
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
/****************************************************************************
File : mphidflash.h
Description : Common header file for all mphidflash sources.
History : 2009-02-19 Phillip Burgess
* Initial implementation
2009-04-16 Phillip Burgess
* Bug fix for non-Intel and 64-bit platforms.
2009-12-26 Thomas Fischl, Dominik Fisch (www.FundF.net)
* Renamed 'ubw32' to 'mphidflash'
2010-12-28 Petr Olivka
* program and verify only data for defined memory areas
* send only even length of data to PIC
License : Copyright (C) 2009 Phillip Burgess
Copyright (C) 2009 Thomas Fischl, Dominik Fisch (www.FundF.net)
Copyright (C) 2010 Petr Olivka
This file is part of 'mphidflash' program.
'mphidflash' 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.
'mphidflash' 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 'mphidflash' source code. If not,
see <http://www.gnu.org/licenses/>.
****************************************************************************/
#ifndef _MPHIDFLASH_H_
#define _MPHIDFLASH_H_
#ifdef DEBUG
#define DEBUGMSG(str) (void)puts(str); fflush(stdout);
#else
#define DEBUGMSG(str)
#endif /* DEBUG */
/* On Intel architectures, can make some crass endianism optimizations */
#if defined(i386) || defined(__x86_64__)
#define bufWrite32(src,pos,val) *(unsigned int *)&src[pos] = val
#define bufRead32(pos) *(unsigned int *)&usbBuf[pos]
#else
#define bufWrite32(src,pos,val) src[pos ] = val & 0xff; \
src[pos + 1] = (val >> 8) & 0xff; \
src[pos + 2] = (val >> 16) & 0xff; \
src[pos + 3] = (val >> 24)
#define bufRead32(pos) ( usbBuf[pos ] | \
(usbBuf[pos + 1] << 8) | \
(usbBuf[pos + 2] << 16) | \
(usbBuf[pos + 3] << 24) )
#endif /* i386 || __x86_64__ */
/* Values derived from Microchip HID Bootloader source */
/* Bootloader commands */
#define QUERY_DEVICE 0x02
#define UNLOCK_CONFIG 0x03
#define ERASE_DEVICE 0x04
#define PROGRAM_DEVICE 0x05
#define PROGRAM_COMPLETE 0x06
#define GET_DATA 0x07
#define RESET_DEVICE 0x08
/* Sub-commands for the ERASE_DEVICE command */
#define UNLOCKCONFIG 0x00
#define LOCKCONFIG 0x01
/* Response types for QUERY_DEVICE command */
#define TypeProgramMemory 0x01
#define TypeEEPROM 0x02
#define TypeConfigWords 0x03
#define TypeEndOfTypeList 0xFF
/* Device family */
#define DEVICE_FAMILY_PIC18 0x01
#define DEVICE_FAMILY_PIC24 0x02
#define DEVICE_FAMILY_PIC32 0x03
/* Error codes returned by various functions */
typedef enum
{
ERR_NONE = 0, /* Success (non-error) */
ERR_CMD_ARG,
ERR_CMD_UNKNOWN,
ERR_DEVICE_NOT_FOUND,
ERR_USB_INIT1,
ERR_USB_INIT2,
ERR_USB_OPEN,
ERR_USB_WRITE,
ERR_USB_READ,
ERR_HEX_OPEN,
ERR_HEX_STAT,
ERR_HEX_MMAP,
ERR_HEX_SYNTAX,
ERR_HEX_CHECKSUM,
ERR_HEX_RECORD,
ERR_VERIFY,
ERR_EOL /* End-of-list, not actual error code */
} ErrorCode;
/* Function prototypes */
extern ErrorCode
hexOpen(char *),
hexWrite(char),
usbOpen(unsigned short,unsigned short),
usbWrite(char,char);
extern void
hexClose(void),
usbClose(void),
hexSetBytesPerAddress(unsigned char);
#pragma pack( push )
#pragma pack( 1 )
typedef struct {
unsigned char Command;
unsigned char PacketDataFieldSize;
unsigned char DeviceFamily;
struct {
unsigned char Type;
unsigned int Address;
unsigned int Length;
} mem[ 6 ];
unsigned char memBlocks;
unsigned char _fill [ 6 ];
} sQuery;
extern sQuery devQuery;
#pragma pack( pop )
#endif /* _MPHIDFLASH_H_ */