Debian Codegraph @

src/arguments.c

raw
1 /*
2 * dfu-programmer
3 *
4 * $Id$
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include <stddef.h>
22 #include <stdint.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdbool.h>
27
28 #include "dfu-device.h"
29 #include "config.h"
30 #include "arguments.h"
31 #include "util.h"
32
33 // Modes used to display the list of targets.
34 #define LIST_STD 0
35 #define LIST_TEX 1
36 #define LIST_HTML 2
37
38 /* Bootloader location options
39 For AVR32 the bootloader is at the bottom of memory space and is included
40 in the total stated memory size. For all other types the bootloader is above
41 the application flash. For AVR and most 8051 types the bootloader space is
42 taken out of the stated memory capacity. For XMega and two of the 8051
43 chips the bootloader space is a separate flash area in addition to the
44 stated memory capacity. Those 8051 chips are also unusual in that the
45 bootloader location is not immediately above the internal flash.
46 In theory you could have a separate bootloader space in low memory but
47 it is not obvious how the addressing would work and none of the Atmel
48 chips do this at present.
49 */
50 #define BL_BASE 0 /* Bootloader at bottom */
51 #define BL_TOP 1 /* Bootloader at top, space included in total memory */
52 #define BL_EXTRA 2 /* Bootloader at top in separate memory area */
53 #define BL_SPECIFIC 3 /* Any value greater than this is a specific start address */
54
55 extern int debug; /* defined in libdfu.c */
56
57 #define ARGUMENTS_DEBUG_THRESHOLD 100
58
59 #define DEBUG(...) dfu_debug( __FILE__, __FUNCTION__, __LINE__, ARGUMENTS_DEBUG_THRESHOLD, __VA_ARGS__ )
60
61 struct option_mapping_structure {
62 const char *name;
63 int32_t value;
64 };
65
66 struct target_mapping_structure {
67 const char *name;
68 enum targets_enum value;
69 atmel_device_class_t device_type;
70 uint16_t chip_id;
71 uint16_t vendor_id;
72 size_t memory_size;
73 size_t bootloader_size;
74 size_t bootloader_location;
75 size_t flash_page_size;
76 size_t eeprom_page_size;
77 size_t eeprom_memory_size;
78 };
79
80 /* NOTE FOR: at90usb1287, at90usb1286, at90usb647, at90usb646, at90usb162, at90usb82
81 *
82 * The actual size of the user-programmable section is limited by the
83 * space needed by the bootloader. The size of the bootloader is set
84 * by BOOTSZ0/BOOTSZ1 fuse bits; here we assume the bootloader is 4kb or 8kb.
85 * The window used for the bootloader is at the top of the of memory.
86 *
87 * VID and PID are the USB identifiers returned by the DFU bootloader.
88 * They are defined by Atmel's bootloader code, and are not in the chip datasheet.
89 * An incomplete list can be found the the various DFU bootloader docs.
90 * If you plug the device in, lsusb or the Windows device manager can tell you
91 * the VID and PID values.
92 */
93
94 /* ----- target specific structures ----------------------------------------- */
95 static struct target_mapping_structure target_map[] = {
96 // START_TARGET_LIST_LINE .. used for autocompletion script
97 // Name ID (arguments.h) DevType PID VID MemSize BootSz BootLoc FPage EPage ESize
98 { "at89c51snd1c", tar_at89c51snd1c, ADC_8051, 0x2FFF, 0x03eb, 0x10000, 0x1000, BL_TOP, 128, 0, 0 },
99 { "at89c51snd2c", tar_at89c51snd2c, ADC_8051, 0x2FFF, 0x03eb, 0x10000, 0x1000, BL_TOP, 128, 0, 0 },
100 { "at89c5130", tar_at89c5130, ADC_8051, 0x2FFD, 0x03eb, 0x04000, 0x0C00, 0xF400, 128, 128, 0x0400 },
101 { "at89c5131", tar_at89c5131, ADC_8051, 0x2FFD, 0x03eb, 0x08000, 0x0C00, 0xF400, 128, 128, 0x0400 },
102 { "at89c5132", tar_at89c5132, ADC_8051, 0x2FFF, 0x03eb, 0x10000, 0x0C00, BL_TOP, 128, 0, 0 },
103 // Name ID (arguments.h) DevType PID VID MemSize BootSz BootLoc FPage EPage ESize
104 { "at90usb1287", tar_at90usb1287, ADC_AVR, 0x2FFB, 0x03eb, 0x20000, 0x2000, BL_TOP, 128, 128, 0x1000 },
105 { "at90usb1286", tar_at90usb1286, ADC_AVR, 0x2FFB, 0x03eb, 0x20000, 0x2000, BL_TOP, 128, 128, 0x1000 },
106 { "at90usb1287-4k", tar_at90usb1287_4k, ADC_AVR, 0x2FFB, 0x03eb, 0x20000, 0x1000, BL_TOP, 128, 128, 0x1000 },
107 { "at90usb1286-4k", tar_at90usb1286_4k, ADC_AVR, 0x2FFB, 0x03eb, 0x20000, 0x1000, BL_TOP, 128, 128, 0x1000 },
108 { "at90usb647", tar_at90usb647, ADC_AVR, 0x2FF9, 0x03eb, 0x10000, 0x2000, BL_TOP, 128, 128, 0x0800 },
109 { "at90usb646", tar_at90usb646, ADC_AVR, 0x2FF9, 0x03eb, 0x10000, 0x2000, BL_TOP, 128, 128, 0x0800 },
110 { "at90usb162", tar_at90usb162, ADC_AVR, 0x2FFA, 0x03eb, 0x04000, 0x1000, BL_TOP, 128, 128, 0x0200 },
111 { "at90usb82", tar_at90usb82, ADC_AVR, 0x2FF7, 0x03eb, 0x02000, 0x1000, BL_TOP, 128, 128, 0x0200 },
112 { "atmega32u6", tar_atmega32u6, ADC_AVR, 0x2FF2, 0x03eb, 0x08000, 0x1000, BL_TOP, 128, 128, 0x0400 },
113 { "atmega32u4", tar_atmega32u4, ADC_AVR, 0x2FF4, 0x03eb, 0x08000, 0x1000, BL_TOP, 128, 128, 0x0400 },
114 { "atmega32u2", tar_atmega32u2, ADC_AVR, 0x2FF0, 0x03eb, 0x08000, 0x1000, BL_TOP, 128, 128, 0x0400 },
115 { "atmega16u4", tar_atmega16u4, ADC_AVR, 0x2FF3, 0x03eb, 0x04000, 0x1000, BL_TOP, 128, 128, 0x0200 },
116 { "atmega16u2", tar_atmega16u2, ADC_AVR, 0x2FEF, 0x03eb, 0x04000, 0x1000, BL_TOP, 128, 128, 0x0200 },
117 { "atmega8u2", tar_atmega8u2, ADC_AVR, 0x2FEE, 0x03eb, 0x02000, 0x1000, BL_TOP, 128, 128, 0x0200 },
118 // Name ID (arguments.h) DevType PID VID MemSize BootSz BootLoc FPage EPage ESize
119 { "at32uc3a0128", tar_at32uc3a0128, ADC_AVR32, 0x2FF8, 0x03eb, 0x20000, 0x2000, BL_BASE, 512, 0, 0 },
120 { "at32uc3a1128", tar_at32uc3a1128, ADC_AVR32, 0x2FF8, 0x03eb, 0x20000, 0x2000, BL_BASE, 512, 0, 0 },
121 { "at32uc3a0256", tar_at32uc3a0256, ADC_AVR32, 0x2FF8, 0x03eb, 0x40000, 0x2000, BL_BASE, 512, 0, 0 },
122 { "at32uc3a1256", tar_at32uc3a1256, ADC_AVR32, 0x2FF8, 0x03eb, 0x40000, 0x2000, BL_BASE, 512, 0, 0 },
123 { "at32uc3a0512", tar_at32uc3a0512, ADC_AVR32, 0x2FF8, 0x03eb, 0x80000, 0x2000, BL_BASE, 512, 0, 0 },
124 { "at32uc3a1512", tar_at32uc3a1512, ADC_AVR32, 0x2FF8, 0x03eb, 0x80000, 0x2000, BL_BASE, 512, 0, 0 },
125 { "at32uc3a0512es", tar_at32uc3a0512es, ADC_AVR32, 0x2FF8, 0x03eb, 0x80000, 0x2000, BL_BASE, 512, 0, 0 },
126 { "at32uc3a1512es", tar_at32uc3a1512es, ADC_AVR32, 0x2FF8, 0x03eb, 0x80000, 0x2000, BL_BASE, 512, 0, 0 },
127 { "at32uc3a364", tar_at32uc3a364, ADC_AVR32, 0x2FF1, 0x03eb, 0x10000, 0x2000, BL_BASE, 512, 0, 0 },
128 { "at32uc3a364s", tar_at32uc3a364s, ADC_AVR32, 0x2FF1, 0x03eb, 0x10000, 0x2000, BL_BASE, 512, 0, 0 },
129 { "at32uc3a3128", tar_at32uc3a3128, ADC_AVR32, 0x2FF1, 0x03eb, 0x20000, 0x2000, BL_BASE, 512, 0, 0 },
130 { "at32uc3a3128s", tar_at32uc3a3128s, ADC_AVR32, 0x2FF1, 0x03eb, 0x20000, 0x2000, BL_BASE, 512, 0, 0 },
131 { "at32uc3a3256", tar_at32uc3a3256, ADC_AVR32, 0x2FF1, 0x03eb, 0x40000, 0x2000, BL_BASE, 512, 0, 0 },
132 { "at32uc3a3256s", tar_at32uc3a3256s, ADC_AVR32, 0x2FF1, 0x03eb, 0x40000, 0x2000, BL_BASE, 512, 0, 0 },
133 { "at32uc3a4256s", tar_at32uc3a4256s, ADC_AVR32, 0x2FF1, 0x03eb, 0x40000, 0x2000, BL_BASE, 512, 0, 0 },
134 // Name ID (arguments.h) DevType PID VID MemSize BootSz BootLoc FPage EPage ESize
135 { "at32uc3b064", tar_at32uc3b064, ADC_AVR32, 0x2FF6, 0x03eb, 0x10000, 0x2000, BL_BASE, 512, 0, 0 },
136 { "at32uc3b164", tar_at32uc3b164, ADC_AVR32, 0x2FF6, 0x03eb, 0x10000, 0x2000, BL_BASE, 512, 0, 0 },
137 { "at32uc3b0128", tar_at32uc3b0128, ADC_AVR32, 0x2FF6, 0x03eb, 0x20000, 0x2000, BL_BASE, 512, 0, 0 },
138 { "at32uc3b1128", tar_at32uc3b1128, ADC_AVR32, 0x2FF6, 0x03eb, 0x20000, 0x2000, BL_BASE, 512, 0, 0 },
139 { "at32uc3b0256", tar_at32uc3b0256, ADC_AVR32, 0x2FF6, 0x03eb, 0x40000, 0x2000, BL_BASE, 512, 0, 0 },
140 { "at32uc3b1256", tar_at32uc3b1256, ADC_AVR32, 0x2FF6, 0x03eb, 0x40000, 0x2000, BL_BASE, 512, 0, 0 },
141 { "at32uc3b0256es", tar_at32uc3b0256es, ADC_AVR32, 0x2FF6, 0x03eb, 0x40000, 0x2000, BL_BASE, 512, 0, 0 },
142 { "at32uc3b1256es", tar_at32uc3b1256es, ADC_AVR32, 0x2FF6, 0x03eb, 0x40000, 0x2000, BL_BASE, 512, 0, 0 },
143 { "at32uc3b0512", tar_at32uc3b0512, ADC_AVR32, 0x2FF6, 0x03eb, 0x80000, 0x2000, BL_BASE, 512, 0, 0 },
144 { "at32uc3b1512", tar_at32uc3b1512, ADC_AVR32, 0x2FF6, 0x03eb, 0x80000, 0x2000, BL_BASE, 512, 0, 0 },
145 // Name ID (arguments.h) DevType PID VID MemSize BootSz BootLoc FPage EPage ESize
146 { "at32uc3c064", tar_at32uc3c064, ADC_AVR32, 0x2FEB, 0x03eb, 0x10000, 0x2000, BL_BASE, 512, 0, 0 },
147 { "at32uc3c0128", tar_at32uc3c0128, ADC_AVR32, 0x2FEB, 0x03eb, 0x20000, 0x2000, BL_BASE, 512, 0, 0 },
148 { "at32uc3c0256", tar_at32uc3c0256, ADC_AVR32, 0x2FEB, 0x03eb, 0x40000, 0x2000, BL_BASE, 512, 0, 0 },
149 { "at32uc3c0512", tar_at32uc3c0512, ADC_AVR32, 0x2FEB, 0x03eb, 0x80000, 0x2000, BL_BASE, 512, 0, 0 },
150 { "at32uc3c164", tar_at32uc3c164, ADC_AVR32, 0x2FEB, 0x03eb, 0x10000, 0x2000, BL_BASE, 512, 0, 0 },
151 { "at32uc3c1128", tar_at32uc3c1128, ADC_AVR32, 0x2FEB, 0x03eb, 0x20000, 0x2000, BL_BASE, 512, 0, 0 },
152 { "at32uc3c1256", tar_at32uc3c1256, ADC_AVR32, 0x2FEB, 0x03eb, 0x40000, 0x2000, BL_BASE, 512, 0, 0 },
153 { "at32uc3c1512", tar_at32uc3c1512, ADC_AVR32, 0x2FEB, 0x03eb, 0x80000, 0x2000, BL_BASE, 512, 0, 0 },
154 { "at32uc3c264", tar_at32uc3c264, ADC_AVR32, 0x2FEB, 0x03eb, 0x10000, 0x2000, BL_BASE, 512, 0, 0 },
155 { "at32uc3c2128", tar_at32uc3c2128, ADC_AVR32, 0x2FEB, 0x03eb, 0x20000, 0x2000, BL_BASE, 512, 0, 0 },
156 { "at32uc3c2256", tar_at32uc3c2256, ADC_AVR32, 0x2FEB, 0x03eb, 0x40000, 0x2000, BL_BASE, 512, 0, 0 },
157 { "at32uc3c2512", tar_at32uc3c2512, ADC_AVR32, 0x2FEB, 0x03eb, 0x80000, 0x2000, BL_BASE, 512, 0, 0 },
158 // Name ID (arguments.h) DevType PID VID MemSize BootSz BootLoc FPage EPage ESize
159 { "atxmega64a1u", tar_atxmega64a1u, ADC_XMEGA, 0x2FE8, 0x03eb, 0x10000, 0x1000, BL_EXTRA, 256, 32, 0x0800 },
160 { "atxmega128a1u", tar_atxmega128a1u, ADC_XMEGA, 0x2FED, 0x03eb, 0x20000, 0x2000, BL_EXTRA, 256, 32, 0x0800 },
161 { "atxmega64a3u", tar_atxmega64a3u, ADC_XMEGA, 0x2FE5, 0x03eb, 0x10000, 0x1000, BL_EXTRA, 256, 32, 0x0800 },
162 { "atxmega128a3u", tar_atxmega128a3u, ADC_XMEGA, 0x2FE6, 0x03eb, 0x20000, 0x2000, BL_EXTRA, 512, 32, 0x0800 },
163 { "atxmega192a3u", tar_atxmega192a3u, ADC_XMEGA, 0x2FE7, 0x03eb, 0x30000, 0x2000, BL_EXTRA, 512, 32, 0x0800 },
164 { "atxmega256a3u", tar_atxmega256a3u, ADC_XMEGA, 0x2FEC, 0x03eb, 0x40000, 0x2000, BL_EXTRA, 512, 32, 0x1000 },
165 { "atxmega16a4u", tar_atxmega16a4u, ADC_XMEGA, 0x2FE3, 0x03eb, 0x04000, 0x1000, BL_EXTRA, 256, 32, 0x0400 },
166 { "atxmega32a4u", tar_atxmega32a4u, ADC_XMEGA, 0x2FE4, 0x03eb, 0x08000, 0x1000, BL_EXTRA, 256, 32, 0x0400 },
167 { "atxmega64a4u", tar_atxmega64a4u, ADC_XMEGA, 0x2FDD, 0x03eb, 0x10000, 0x1000, BL_EXTRA, 256, 32, 0x0800 },
168 { "atxmega128a4u", tar_atxmega128a4u, ADC_XMEGA, 0x2FDE, 0x03eb, 0x20000, 0x2000, BL_EXTRA, 256, 32, 0x0800 },
169 { "atxmega256a3bu", tar_atxmega256a3bu, ADC_XMEGA, 0x2FE2, 0x03eb, 0x40000, 0x2000, BL_EXTRA, 512, 32, 0x1000 },
170 // Name ID (arguments.h) DevType PID VID MemSize BootSz BootLoc FPage EPage ESize
171 { "atxmega64b1", tar_atxmega64b1, ADC_XMEGA, 0x2FE1, 0x03eb, 0x10000, 0x1000, BL_EXTRA, 256, 32, 0x0800 },
172 { "atxmega128b1", tar_atxmega128b1, ADC_XMEGA, 0x2FEA, 0x03eb, 0x20000, 0x2000, BL_EXTRA, 256, 32, 0x0800 },
173 { "atxmega64b3", tar_atxmega64b3, ADC_XMEGA, 0x2FDF, 0x03eb, 0x10000, 0x1000, BL_EXTRA, 256, 32, 0x0800 },
174 { "atxmega128b3", tar_atxmega128b3, ADC_XMEGA, 0x2FE0, 0x03eb, 0x20000, 0x2000, BL_EXTRA, 256, 32, 0x0800 },
175 // Name ID (arguments.h) DevType PID VID MemSize BootSz BootLoc FPage EPage ESize
176 { "atxmega64c3", tar_atxmega64c3, ADC_XMEGA, 0x2FD6, 0x03eb, 0x10000, 0x1000, BL_EXTRA, 256, 32, 0x0800 },
177 { "atxmega128c3", tar_atxmega128c3, ADC_XMEGA, 0x2FD7, 0x03eb, 0x20000, 0x2000, BL_EXTRA, 512, 32, 0x0800 },
178 { "atxmega256c3", tar_atxmega256c3, ADC_XMEGA, 0x2FDA, 0x03eb, 0x40000, 0x2000, BL_EXTRA, 512, 32, 0x1000 },
179 { "atxmega384c3", tar_atxmega384c3, ADC_XMEGA, 0x2FDB, 0x03eb, 0x60000, 0x2000, BL_EXTRA, 512, 32, 0x1000 },
180 { "atxmega16c4", tar_atxmega16c4, ADC_XMEGA, 0x2FD8, 0x03eb, 0x4000, 0x1000, BL_EXTRA, 256, 32, 0x400 },
181 { "atxmega32c4", tar_atxmega32c4, ADC_XMEGA, 0x2FD9, 0x03eb, 0x8000, 0x1000, BL_EXTRA, 256, 32, 0x400 },
182 // Name ID (arguments.h) DevType PID VID MemSize BootSz BootLoc FPage EPage ESize
183 // NOTE : that support for these targets is experimental but has been tested with stm32f4 chips on ubuntu
184 { "stm32f4_B", tar_stm32f4_B, DC_STM32, 0xdf11, 0x0483, 0x20000, 0x0000, BL_EXTRA, 512, 0, 0 },
185 { "stm32f4_C", tar_stm32f4_C, DC_STM32, 0xdf11, 0x0483, 0x40000, 0x0000, BL_EXTRA, 512, 0, 0 },
186 { "stm32f4_E", tar_stm32f4_E, DC_STM32, 0xdf11, 0x0483, 0x80000, 0x0000, BL_EXTRA, 512, 0, 0 },
187 { "stm32f4_G", tar_stm32f4_G, DC_STM32, 0xdf11, 0x0483, 0x100000,0x0000, BL_EXTRA, 512, 0, 0 },
188 { NULL }
189 // END_TARGET_LIST_LINE .. used for autocompletion script
190 };
191
192 /* ----- command specific structures ---------------------------------------- */
193 static struct option_mapping_structure command_map[] = {
194 { "configure", com_configure },
195 { "read", com_read },
196 { "dump", com_dump },
197 { "dump-eeprom", com_edump },
198 { "dump-user", com_udump },
199 { "erase", com_erase },
200 { "flash", com_flash },
201 { "flash-user", com_user },
202 { "flash-eeprom", com_eflash },
203 { "get", com_get },
204 { "getfuse", com_getfuse },
205 { "launch", com_launch },
206 { "reset", com_reset },
207 { "setfuse", com_setfuse },
208 { "setsecure", com_setsecure },
209 { "start", com_start_app },
210 { "bin2hex", com_bin2hex },
211 { "hex2bin", com_hex2bin },
212 { NULL }
213 };
214
215 /* ----- configure specific structures -------------------------------------- */
216 static struct option_mapping_structure configure_map[] = {
217 { "BSB", conf_BSB },
218 { "SBV", conf_SBV },
219 { "SSB", conf_SSB },
220 { "EB", conf_EB },
221 { "HSB", conf_HSB },
222 { NULL }
223 };
224
225 /* ----- get specific structures -------------------------------------- */
226 static struct option_mapping_structure get_map[] = {
227 { "bootloader-version", get_bootloader },
228 { "ID1", get_ID1 },
229 { "ID2", get_ID2 },
230 { "BSB", get_BSB },
231 { "SBV", get_SBV },
232 { "SSB", get_SSB },
233 { "EB", get_EB },
234 { "manufacturer", get_manufacturer },
235 { "family", get_family },
236 { "product-name", get_product_name },
237 { "product-revision", get_product_rev },
238 { "HSB", get_HSB },
239 { NULL }
240 };
241
242 /* ----- getfuse specific structures ---------------------------------- */
243 static struct option_mapping_structure getfuse_map[] = {
244 { "LOCK", get_lock },
245 { "EPFL", get_epfl },
246 { "BOOTPROT", get_bootprot },
247 { "BODLEVEL", get_bodlevel },
248 { "BODHYST", get_bodhyst },
249 { "BODEN", get_boden },
250 { "ISP_BOD_EN", get_isp_bod_en },
251 { "ISP_IO_COND_EN", get_isp_io_cond_en },
252 { "ISP_FORCE", get_isp_force },
253 { NULL }
254 };
255
256 /* ----- setfuse specific structures ---------------------------------- */
257 static struct option_mapping_structure setfuse_map[] = {
258 { "LOCK", set_lock },
259 { "EPFL", set_epfl },
260 { "BOOTPROT", set_bootprot },
261 { "BODLEVEL", set_bodlevel },
262 { "BODHYST", set_bodhyst },
263 { "BODEN", set_boden },
264 { "ISP_BOD_EN", set_isp_bod_en },
265 { "ISP_IO_COND_EN", set_isp_io_cond_en },
266 { "ISP_FORCE", set_isp_force },
267 { NULL }
268 };
269
270 static void list_targets(int mode)
271 {
272 struct target_mapping_structure *map = NULL;
273 int col = 0;
274 int group_count = 0;
275 atmel_device_class_t device_type = 0;
276 const char *dev_type_name;
277
278 map = target_map;
279
280 while( 0 != *((int32_t *) map) ) {
281 if( device_type != map->device_type ) {
282 device_type = map->device_type;
283 switch( device_type ) {
284 case ADC_8051: dev_type_name = "8051"; break;
285 case ADC_AVR: dev_type_name = "AVR"; break;
286 case ADC_AVR32: dev_type_name = "AVR32"; break;
287 case ADC_XMEGA: dev_type_name = "XMEGA"; break;
288 case DC_STM32: dev_type_name = "STM32F4"; break;
289 default: dev_type_name = NULL; break;
290 }
291 if( dev_type_name != NULL ) {
292 if( LIST_TEX == mode ) {
293 if( map != target_map )
294 fprintf( stdout, "\n" );
295 fprintf( stdout, ".IP \"%s based controllers:\"\n", dev_type_name );
296 } else if( LIST_HTML == mode ) {
297 if( map != target_map )
298 fprintf( stdout, "\n</p>\n" );
299 fprintf( stdout, "<h3>%s based controllers:</h3>\n<p>\n", dev_type_name );
300 } else {
301 if( 0 != col )
302 fprintf( stdout, "\n" );
303 fprintf( stdout, "%s based controllers:\n", dev_type_name );
304 }
305 group_count = 0;
306 col = 0;
307 }
308 }
309 if( LIST_STD == mode ) {
310 if( 0 == col ) {
311 fprintf( stdout, " " );
312 }
313 fprintf( stdout, " %-16s", map->name );
314 if( 4 == ++col ) {
315 fprintf( stdout, "\n" );
316 col = 0;
317 }
318 } else {
319 if( 0 == col ) {
320 if( 0 != group_count )
321 fprintf( stdout, ",\n" );
322 } else {
323 fprintf( stdout, ", " );
324 }
325 fprintf( stdout, "%s", map->name );
326 if( 4 == ++col ) {
327 col = 0;
328 }
329 }
330 map++;
331 group_count++;
332 }
333 if( 0 != col )
334 fprintf( stdout, "\n" );
335 if( LIST_HTML == mode )
336 fprintf( stdout, "</p>\n" );
337 }
338
339 static void basic_help()
340 {
341 fprintf( stderr, PACKAGE_STRING "\n");
342 fprintf( stderr, PACKAGE_URL "\n" );
343 fprintf( stderr, "Type 'dfu-programmer --help' for a list of commands\n" );
344 fprintf( stderr, " 'dfu-programmer --targets' to list supported target devices\n" );
345 }
346
347 static void usage()
348 {
349 fprintf( stderr, PACKAGE_STRING "\n");
350 fprintf( stderr, PACKAGE_URL "\n" );
351 fprintf( stderr, "Usage: dfu-programmer target[:usb-bus,usb-addr] command [options] "
352 "[global-options] [file|data]\n\n" );
353 fprintf( stderr, "global-options:\n"
354 " --quiet\n"
355 " --debug level (level is an integer specifying level of detail)\n"
356 " Global options can be used with any command and must come\n"
357 " after the command and before any file or data value\n" );
358 fprintf( stderr, "\n" );
359 fprintf( stderr, "command summary:\n" );
360 fprintf( stderr, " launch [--no-reset]\n" );
361 fprintf( stderr, " read [--force] [--bin] [(flash)|--user|--eeprom]\n" );
362 fprintf( stderr, " erase [--force] [--suppress-validation]\n" );
363 fprintf( stderr, " flash [--force] [(flash)|--user|--eeprom]\n"
364 " [--suppress-validation]\n"
365 " [--suppress-bootloader-mem]\n"
366 " [--validate-first]\n"
367 " [--erase-first]\n"
368 " [--ignore-outside]\n"
369 " [--serial=hexdigits:offset] {file|STDIN}\n" );
370 fprintf( stderr, " setsecure\n" );
371 fprintf( stderr, " configure {BSB|SBV|SSB|EB|HSB}"
372 " [--suppress-validation] data\n" );
373 fprintf( stderr, " get {bootloader-version|ID1|ID2|BSB|SBV|SSB|EB|\n"
374 " manufacturer|family|product-name|\n"
375 " product-revision|HSB}\n" );
376 fprintf( stderr, " getfuse {LOCK|EPFL|BOOTPROT|BODLEVEL|BODHYST|\n"
377 " BODEN|ISP_BOD_EN|ISP_IO_COND_EN|\n"
378 " ISP_FORCE}\n" );
379 fprintf( stderr, " setfuse {LOCK|EPFL|BOOTPROT|BODLEVEL|BODHYST|\n"
380 " BODEN|ISP_BOD_EN|ISP_IO_COND_EN|\n"
381 " ISP_FORCE} data\n" );
382 fprintf( stderr, "\n" );
383 fprintf( stderr, "additional details:\n" );
384 fprintf( stderr,
385 " launch: Launch from the bootloader into the main program using a watchdog\n"
386 " reset. To jump directly into the main program use --no-reset.\n");
387 fprintf( stderr,
388 " read: Read the program memory in flash and output non-blank pages in ihex\n"
389 " format. Use --force to output the entire memory and --bin for binary\n"
390 " output. User page and eeprom are selected using --user and --eeprom\n");
391 fprintf( stderr,
392 " erase: Erase memory contents if the chip is not blank or always with --force\n");
393 fprintf( stderr,
394 " flash: Flash a program onto device flash memory. EEPROM and user page are\n"
395 " selected using --eeprom|--user flags. Use --force to ignore warning\n"
396 " when data exists in target memory region. Bootloader configuration\n"
397 " uses last 4 to 8 bytes of user page, --force always required here.\n");
398 fprintf( stderr, "Note: version 0.6.1 commands still supported.\n");
399 }
400
401
402 static int32_t assign_option( int32_t *arg,
403 char *value,
404 struct option_mapping_structure *map )
405 {
406 while( 0 != *((int32_t *) map) ) {
407 if( 0 == strcasecmp(value, map->name) ) {
408 *arg = map->value;
409 return 0;
410 }
411
412 map++;
413 }
414
415 return -1;
416 }
417
418 static int32_t assign_target( struct programmer_arguments *args,
419 char *value,
420 struct target_mapping_structure *map )
421 {
422 while( 0 != *((int32_t *) map) ) {
423 size_t name_len = strlen(map->name);
424 if( 0 == strncasecmp(value, map->name, name_len)
425 && (value[name_len] == '\0'
426 || value[name_len] == ':')) {
427 args->target = map->value;
428 args->chip_id = map->chip_id;
429 args->vendor_id = map->vendor_id;
430 args->bus_id = 0;
431 args->device_address = 0;
432 if (value[name_len] == ':') {
433 /* The target name includes USB bus and address info.
434 * This is used to differentiate between multiple dfu
435 * devices with the same vendor/chip ID numbers. By
436 * specifying the bus and address, multiple units can
437 * be programmed at one time.
438 */
439 int bus = 0;
440 int address = 0;
441 if( 2 != sscanf(&value[name_len+1], "%i,%i", &bus, &address) )
442 return -1;
443 if (bus <= 0) return -1;
444 if (address < 0) return -1;
445 args->bus_id = bus;
446 args->device_address = address;
447 }
448 args->device_type = map->device_type;
449 args->eeprom_memory_size = map->eeprom_memory_size;
450 args->flash_page_size = map->flash_page_size;
451 args->eeprom_page_size = map->eeprom_page_size;
452 if ( map->bootloader_location > BL_SPECIFIC ) {
453 /* Bootloader space at a specific location, but does not
454 lie immediately above the application flash area.
455 */
456 args->flash_address_bottom = 0;
457 args->flash_address_top = map->memory_size - 1;
458 args->bootloader_bottom = map->bootloader_location;
459 args->bootloader_top = map->bootloader_location + map->bootloader_size - 1;
460 args->memory_address_bottom = args->flash_address_bottom;
461 args->memory_address_top = args->bootloader_top;
462 } else if( BL_BASE == map->bootloader_location ) {
463 /* Bootloader space is at bottom of memory and is
464 included within stated memory space. */
465 args->bootloader_bottom = 0;
466 args->bootloader_top = map->bootloader_size - 1;
467 args->flash_address_bottom = map->bootloader_size;
468 args->flash_address_top = map->memory_size - 1;
469 args->memory_address_bottom = args->bootloader_bottom;
470 args->memory_address_top = args->flash_address_top;
471 } else {
472 /* Bootloader space is at top of memory above application area. */
473 if( BL_EXTRA == map->bootloader_location ) {
474 /* Bootloader space is additional to stated memory space. */
475 args->bootloader_bottom = map->memory_size;
476 } else {
477 /* Bootloader space is included within stated memory space. */
478 args->bootloader_bottom = map->memory_size - map->bootloader_size;
479 }
480 args->flash_address_bottom = 0;
481 args->flash_address_top = args->bootloader_bottom - 1;
482 args->bootloader_top = args->bootloader_bottom + map->bootloader_size - 1;
483 args->memory_address_bottom = args->flash_address_bottom;
484 args->memory_address_top = args->bootloader_top;
485 }
486
487 switch( args->device_type ) {
488 case ADC_8051:
489 strncpy( args->device_type_string, "8051",
490 DEVICE_TYPE_STRING_MAX_LENGTH );
491 args->initial_abort = false;
492 //args->honor_interfaceclass = true;
493 break;
494 case ADC_AVR:
495 strncpy( args->device_type_string, "AVR",
496 DEVICE_TYPE_STRING_MAX_LENGTH );
497 args->initial_abort = true;
498 //args->honor_interfaceclass = false;
499 break;
500 case ADC_AVR32:
501 strncpy( args->device_type_string, "AVR32",
502 DEVICE_TYPE_STRING_MAX_LENGTH );
503 args->initial_abort = false;
504 //args->honor_interfaceclass = true;
505 break;
506 case ADC_XMEGA:
507 strncpy( args->device_type_string, "XMEGA",
508 DEVICE_TYPE_STRING_MAX_LENGTH );
509 args->initial_abort = true;
510 //args->honor_interfaceclass = false;
511 break;
512 case DC_STM32:
513 strncpy( args->device_type_string, "STM32",
514 DEVICE_TYPE_STRING_MAX_LENGTH );
515 break;
516 default :
517 // cSpell:words UNKNO
518 strncpy( args->device_type_string, "UNKNO",
519 DEVICE_TYPE_STRING_MAX_LENGTH );
520 break;
521 }
522 /* There have been several reports on the mailing list of dfu-programmer
523 reporting "No device present" when there clearly is. It seems Atmel's
524 bootloader has changed (or is buggy) and doesn't report interface class
525 and subclass the way is did before. However we have already matched
526 VID and PID so why would we worry about this? Don't use the device-
527 specific value, just ignore the error for all device types.
528 */
529 args->honor_interfaceclass = false;
530 return 0;
531 }
532
533 map++;
534 }
535
536 return -1;
537 }
538
539 static int32_t assign_global_options( struct programmer_arguments *args,
540 const size_t argc,
541 char **argv )
542 {
543 size_t i = 0;
544
545 /* Find '--quiet' if it is here */
546 for( i = 0; i < argc; i++ ) {
547 if( 0 == strcmp("--quiet", argv[i]) ) {
548 *argv[i] = '\0';
549 args->quiet = 1;
550 break;
551 }
552 }
553
554 /* Find '--suppress-bootloader-mem' if it is here */
555 for( i = 0; i < argc; i++ ) {
556 if( 0 == strcmp("--suppress-bootloader-mem", argv[i]) ) {
557 *argv[i] = '\0';
558 args->suppressBootloader = 1;
559 break;
560 }
561 }
562
563 /* Find '--suppress-validation' if it is here - even though it is not
564 * used by all this is easier. */
565 for( i = 0; i < argc; i++ ) {
566 if( 0 == strcmp("--suppress-validation", argv[i]) ) {
567 *argv[i] = '\0';
568
569 switch( args->command ) {
570 case com_configure:
571 args->com_configure_data.suppress_validation = 1;
572 break;
573 case com_erase:
574 args->com_erase_data.suppress_validation = 1;
575 break;
576 case com_flash:
577 case com_eflash:
578 case com_user:
579 args->com_flash_data.suppress_validation = 1;
580 break;
581 default:
582 /* not supported. */
583 return -1;
584 }
585 break;
586 }
587 }
588
589 /* Find '--validate-first' if it is here - even though it is not
590 * used by all this is easier. */
591 for( i = 0; i < argc; i++ ) {
592 if( 0 == strcmp("--validate-first", argv[i]) ) {
593 *argv[i] = '\0';
594
595 switch( args->command ) {
596 case com_flash:
597 case com_eflash:
598 case com_user:
599 args->com_flash_data.validate_first = 1;
600 break;
601 default:
602 /* not supported. */
603 return -1;
604 }
605 break;
606 }
607 }
608
609 /* Find '--erase-first' if it is here - even though it is not
610 * used by all this is easier. */
611 for( i = 0; i < argc; i++ ) {
612 if( 0 == strcmp("--erase-first", argv[i]) ) {
613 *argv[i] = '\0';
614
615 switch( args->command ) {
616 case com_flash:
617 args->com_flash_data.erase_first = 1;
618 break;
619 default:
620 /* not supported. */
621 return -1;
622 }
623 break;
624 }
625 }
626
627 /* Find '--ignore-outside' if it is here - even though it is not
628 * used by all this is easier. */
629 for( i = 0; i < argc; i++ ) {
630 if( 0 == strcmp("--ignore-outside", argv[i]) ) {
631 *argv[i] = '\0';
632
633 switch( args->command ) {
634 case com_flash:
635 case com_eflash:
636 case com_user:
637 args->com_flash_data.ignore_outside = 1;
638 break;
639 default:
640 /* not supported. */
641 return -1;
642 }
643 break;
644 }
645 }
646
647 /* Find '--bin' for read binary */
648 for( i = 0; i < argc; i++ ) {
649 if( 0 == strcmp("--bin", argv[i]) ) {
650 *argv[i] = '\0';
651
652 switch( args->command ) {
653 case com_read:
654 case com_dump:
655 case com_edump:
656 case com_udump:
657 args->com_read_data.bin = 1;
658 break;
659 default:
660 /* not supported. */
661 return -1;
662 }
663 break;
664 }
665 }
666
667 /* Find '--user' for the user page segment */
668 for( i = 0; i < argc; i++ ) {
669 if( 0 == strcmp("--user", argv[i]) ) {
670 *argv[i] = '\0';
671 switch( args->command ) {
672 case com_read:
673 case com_udump:
674 args->com_read_data.segment = mem_user;
675 break;
676 case com_flash:
677 case com_user:
678 args->com_flash_data.segment = mem_user;
679 break;
680 case com_bin2hex:
681 args->com_convert_data.segment = mem_user;
682 break;
683 default:
684 /* not supported. */
685 return -1;
686 }
687 break;
688 }
689 }
690
691 /* Find '--eeprom' for the eeprom page segment */
692 for( i = 0; i < argc; i++ ) {
693 if( 0 == strcmp("--eeprom", argv[i]) ) {
694 *argv[i] = '\0';
695 switch( args->command ) {
696 case com_read:
697 case com_udump:
698 args->com_read_data.segment = mem_eeprom;
699 break;
700 case com_flash:
701 case com_user:
702 args->com_flash_data.segment = mem_eeprom;
703 break;
704 case com_bin2hex:
705 args->com_convert_data.segment = mem_eeprom;
706 break;
707 default:
708 /* not supported. */
709 return -1;
710 }
711 break;
712 }
713 }
714
715 /* Find '--force' if it is here - even though it is not
716 * used by all this is easier. */
717 for( i = 0; i < argc; i++ ) {
718 if( 0 == strcmp("--force", argv[i]) ) {
719 *argv[i] = '\0';
720 switch( args->command ) {
721 case com_flash :
722 case com_eflash :
723 case com_user :
724 args->com_flash_data.force = true;
725 break;
726 case com_read :
727 args->com_read_data.force = true;
728 break;
729 case com_erase :
730 args->com_erase_data.force = true;
731 break;
732 default:
733 // not supported
734 return -1;
735 }
736 break;
737 }
738 }
739
740 /* Find '--no-reset' if it is here - even though it is not
741 * used by all this is easier. */
742 for( i = 0; i < argc; i++ ) {
743 if( 0 == strcmp("--no-reset", argv[i]) ) {
744 *argv[i] = '\0';
745
746 if ( args->command == com_launch ) {
747 args->com_launch_config.noreset = true;
748 } else {
749 // not supported
750 return -1;
751 }
752 break;
753 }
754 }
755
756 /* Find '--debug' if it is here */
757 for( i = 0; i < argc; i++ ) {
758 if( 0 == strncmp("--debug", argv[i], 7) ) {
759
760 if( 0 == strncmp("--debug=", argv[i], 8) ) {
761 if( 1 != sscanf(argv[i], "--debug=%i", &debug) )
762 return -2;
763 } else {
764 if( (i+1) >= argc )
765 return -3;
766
767 if( 1 != sscanf(argv[i+1], "%i", &debug) )
768 return -4;
769
770 *argv[i+1] = '\0';
771 }
772 *argv[i] = '\0';
773 break;
774 }
775 }
776
777 /* Find '--serial=<hexdigit+>:<offset>' */
778 for( i = 0; i < argc; i++ ) {
779 if( 0 == strncmp("--serial=", argv[i], 9) ) {
780 *argv[i] = '\0';
781
782 switch( args->command ) {
783 case com_flash:
784 case com_eflash:
785 case com_user: {
786 char *hexdigits = &argv[i][9];
787 char *offset_start = hexdigits;
788 size_t num_digits = 0;
789 int16_t *serial_data = NULL;
790 long serial_offset = 0;
791 size_t j = 0;
792 char buffer[3] = {0,0,0};
793 while (*offset_start != ':') {
794 char c = *offset_start;
795 if ('\0' == c) {
796 return -1;
797 } else if (('0' <= c && c <= '9')
798 || ('a' <= c && c <= 'f')
799 || ('A' <= c && c <= 'F')) {
800 ++offset_start;
801 } else {
802 fprintf(stderr, "other character: '%c'\n", *offset_start);
803 return -1;
804 }
805 }
806 num_digits = offset_start - hexdigits;
807 if (num_digits & 1) {
808 fprintf(stderr,"There must be an even number of hexdigits in the serial data\n");
809 return -1;
810 }
811 *offset_start++ = '\0';
812 if( 1 != sscanf(offset_start, "%ld", &serial_offset) ) {
813 fprintf(stderr, "sscanf failed\n");
814 return -1;
815 }
816 serial_data = (int16_t *) malloc( (num_digits/2) * sizeof(int16_t) );
817 for (j=0; j < num_digits; j+=2) {
818 int data;
819 buffer[0] = hexdigits[j];
820 buffer[1] = hexdigits[j+1];
821 buffer[2] = 0;
822 if( 1 != sscanf(buffer, "%02x", &data) ) {
823 fprintf(stderr, "sscanf failed with buffer: %s\n", buffer);
824 return -1;
825 }
826 serial_data[j/2] = (int16_t)data;
827 }
828 args->com_flash_data.serial_data = serial_data;
829 args->com_flash_data.serial_offset = serial_offset;
830 args->com_flash_data.serial_length = num_digits/2;
831 break;
832 }
833 default:
834 /* not supported. */
835 fprintf(stderr,"command did not match: %d flash: %d\n", args->command, com_flash);
836 return -1;
837 }
838 fprintf(stderr, "Success getting serial number\n");
839 break;
840 }
841 }
842
843 return 0;
844 }
845
846 static int32_t assign_com_setfuse_option( struct programmer_arguments *args,
847 const int32_t parameter,
848 char *value )
849 {
850 /* name & value */
851 if( 0 == parameter ) {
852 /* name */
853 if( 0 != assign_option((int32_t *) &(args->com_setfuse_data.name),
854 value, setfuse_map) )
855 {
856 return -1;
857 }
858 } else {
859 int32_t temp = 0;
860 /* value */
861 if( 1 != sscanf(value, "%i", &(temp)) )
862 return -2;
863
864 /* ensure the range is greater than 0 */
865 if( temp < 0 )
866 return -3;
867
868 args->com_setfuse_data.value = temp;
869 }
870
871 return 0;
872 }
873
874 static int32_t assign_com_configure_option( struct programmer_arguments *args,
875 const int32_t parameter,
876 char *value )
877 {
878 /* name & value */
879 if( 0 == parameter ) {
880 /* name */
881 if( 0 != assign_option((int32_t *) &(args->com_configure_data.name),
882 value, configure_map) )
883 {
884 return -1;
885 }
886 } else {
887 int32_t temp = 0;
888 /* value */
889 if( 1 != sscanf(value, "%i", &(temp)) )
890 return -2;
891
892 /* ensure the range is greater than 0 */
893 if( temp < 0 )
894 return -3;
895
896 args->com_configure_data.value = temp;
897 }
898
899 return 0;
900 }
901
902 static int32_t assign_com_flash_option( struct programmer_arguments *args,
903 const int32_t parameter,
904 char *value )
905 {
906 /* file */
907 args->com_flash_data.original_first_char = *value;
908 args->com_flash_data.file = value;
909
910 return 0;
911 }
912
913 static int32_t assign_com_convert_option( struct programmer_arguments *args,
914 const int32_t parameter,
915 char *value )
916 {
917 /* file */
918 args->com_convert_data.original_first_char = *value;
919 args->com_convert_data.file = value;
920
921 return 0;
922 }
923
924
925 static int32_t assign_com_getfuse_option( struct programmer_arguments *args,
926 const int32_t parameter,
927 char *value )
928 {
929 /* name */
930 if( 0 != assign_option((int32_t *) &(args->com_getfuse_data.name),
931 value, getfuse_map) )
932 {
933 return -1;
934 }
935
936 return 0;
937 }
938
939 static int32_t assign_com_get_option( struct programmer_arguments *args,
940 const int32_t parameter,
941 char *value )
942 {
943 /* name */
944 if( 0 != assign_option((int32_t *) &(args->com_get_data.name),
945 value, get_map) )
946 {
947 return -1;
948 }
949
950 return 0;
951 }
952
953 static int32_t assign_command_options( struct programmer_arguments *args,
954 const size_t argc,
955 char **argv )
956 {
957 size_t i = 0;
958 int32_t param = 0;
959 int32_t required_params = 0;
960
961 /* Deal with all remaining command-specific arguments. */
962 for( i = 0; i < argc; i++ ) {
963 if( '\0' == *argv[i] )
964 continue;
965
966 switch( args->command ) {
967 case com_configure:
968 required_params = 2;
969 if( 0 != assign_com_configure_option(args, param, argv[i]) )
970 return -1;
971 break;
972
973 case com_setfuse:
974 required_params = 2;
975 if( 0 != assign_com_setfuse_option(args, param, argv[i]) )
976 return -1;
977 break;
978
979 case com_flash:
980 case com_eflash:
981 case com_user:
982 required_params = 1;
983 if( 0 != assign_com_flash_option(args, param, argv[i]) )
984 return -3;
985 break;
986
987 case com_bin2hex:
988 case com_hex2bin:
989 required_params = 1;
990 if( 0 != assign_com_convert_option(args, param, argv[i]) )
991 return -3;
992 break;
993
994 case com_getfuse:
995 required_params = 1;
996 if( 0 != assign_com_getfuse_option(args, param, argv[i]) )
997 return -4;
998 break;
999
1000 case com_get:
1001 required_params = 1;
1002 if( 0 != assign_com_get_option(args, param, argv[i]) )
1003 return -4;
1004 break;
1005
1006 default:
1007 return -5;
1008 }
1009
1010 *argv[i] = '\0';
1011 param++;
1012 }
1013
1014 if( required_params != param )
1015 return -6;
1016
1017 return 0;
1018 }
1019
1020 static void print_args( struct programmer_arguments *args )
1021 {
1022 const char *command = "(unknown)";
1023 const char *target = "(unknown)";
1024 size_t i;
1025
1026 for( i = 0; i < sizeof(target_map) / sizeof(target_map[0]); i++ ) {
1027 if( args->target == target_map[i].value ) {
1028 target = target_map[i].name;
1029 break;
1030 }
1031 }
1032
1033 for( i = 0; i < sizeof(command_map) / sizeof(command_map[0]); i++ ) {
1034 if( args->command == command_map[i].value ) {
1035 command = command_map[i].name;
1036 break;
1037 }
1038 }
1039
1040 fprintf( stderr, " target: %s\n", target );
1041 fprintf( stderr, " chip_id: 0x%04x\n", args->chip_id );
1042 fprintf( stderr, " vendor_id: 0x%04x\n", args->vendor_id );
1043 fprintf( stderr, " command: %s\n", command );
1044 fprintf( stderr, " quiet: %s\n", (0 == args->quiet) ? "false" : "true" );
1045 fprintf( stderr, " debug: %d\n", debug );
1046 fprintf( stderr, "device_type: %s\n", args->device_type_string );
1047 fprintf( stderr, "------ command specific below ------\n" );
1048
1049 switch( args->command ) {
1050 case com_configure:
1051 fprintf( stderr, " name: %d\n", args->com_configure_data.name );
1052 fprintf( stderr, " validate: %s\n",
1053 (args->com_configure_data.suppress_validation) ?
1054 "false" : "true" );
1055 fprintf( stderr, " value: %d\n", args->com_configure_data.value );
1056 break;
1057 case com_erase:
1058 fprintf( stderr, " validate: %s\n",
1059 (args->com_erase_data.suppress_validation) ?
1060 "false" : "true" );
1061 break;
1062 case com_flash:
1063 case com_eflash:
1064 case com_user:
1065 fprintf( stderr, " validate: %s\n",
1066 (args->com_flash_data.suppress_validation) ?
1067 "false" : "true" );
1068 fprintf( stderr, " hex file: %s\n", args->com_flash_data.file );
1069 break;
1070 case com_get:
1071 fprintf( stderr, " name: %d\n", args->com_get_data.name );
1072 break;
1073 case com_launch:
1074 fprintf( stderr, " no-reset: %d\n", args->com_launch_config.noreset );
1075 break;
1076 default:
1077 break;
1078 }
1079 fprintf( stderr, "\n" );
1080 fflush( stdout );
1081 }
1082
1083 static int32_t execute_hex2bin( struct programmer_arguments *args ) {
1084 int32_t retval = -1;
1085 uint32_t i;
1086 intel_buffer_out_t bout;
1087 size_t memory_size;
1088 size_t page_size;
1089 uint32_t target_offset = 0;
1090
1091 memory_size = args->memory_address_top + 1;
1092 page_size = args->flash_page_size;
1093
1094 // ----------------- CONVERT HEX FILE TO BINARY -------------------------
1095 if( 0 != intel_init_buffer_out(&bout, memory_size, page_size) ) {
1096 DEBUG("ERROR initializing a buffer.\n");
1097 goto error;
1098 }
1099
1100 if( 0!= intel_hex_to_buffer( args->com_convert_data.file, &bout,
1101 target_offset, args->quiet ) ) {
1102 DEBUG( "Something went wrong with creating the memory image.\n" );
1103 goto error;
1104 }
1105
1106 if( !args->quiet )
1107 fprintf( stderr, "Dumping 0x%X bytes from address offset 0x%X.\n",
1108 bout.info.data_end + 1, target_offset );
1109 for( i = 0; i <= bout.info.data_end; i++ ) {
1110 fprintf( stdout, "%c", bout.data[i] <= 0xFF ? bout.data[i] & 0xFF : 0xFF);
1111 }
1112
1113 fflush( stdout );
1114
1115 retval = 0;
1116
1117 error:
1118 if( NULL != bout.data ) {
1119 free( bout.data );
1120 bout.data = NULL;
1121 }
1122
1123 return retval;
1124 }
1125
1126 static int32_t execute_bin2hex( struct programmer_arguments *args ) {
1127 int32_t retval = -1; // return value for this fcn
1128 intel_buffer_in_t buin; // buffer in for storing read mem
1129 enum atmel_memory_unit_enum mem_segment = args->com_convert_data.segment;
1130 size_t mem_size = 0;
1131 size_t page_size = 0;
1132 uint32_t target_offset = 0; // address offset on the target device
1133 // NOTE: target_offset may not be set appropriately for device
1134 // classes other than ADC_AVR32
1135 FILE *fp = NULL;
1136 char *filename = args->com_convert_data.file;
1137
1138 if( ADC_AVR32 == args->device_type ) {
1139 target_offset = 0x80000000;
1140 }
1141
1142 switch( mem_segment ) {
1143 case mem_flash:
1144 mem_size = args->memory_address_top + 1;
1145 page_size = args->flash_page_size;
1146 break;
1147 case mem_eeprom:
1148 mem_size = args->eeprom_memory_size;
1149 page_size = args->eeprom_page_size;
1150 break;
1151 case mem_user:
1152 mem_size = args->flash_page_size;
1153 page_size = args->flash_page_size;
1154 target_offset = 0x80800000;
1155 break;
1156 default:
1157 fprintf( stderr, "Dump not currently supported for this memory.\n" );
1158 goto error;
1159 }
1160
1161 if( 0 != intel_init_buffer_in(&buin, mem_size, page_size) ) {
1162 DEBUG("ERROR initializing a buffer.\n");
1163 goto error;
1164 }
1165
1166 if( mem_segment == mem_flash ) {
1167 buin.info.data_start = args->flash_address_bottom;
1168 buin.info.data_end = args->flash_address_top;
1169 }
1170
1171 if( NULL == filename ) {
1172 if( !args->quiet ) fprintf( stderr, "Invalid filename.\n" );
1173 retval = -2;
1174 goto error;
1175 }
1176
1177 if( 0 == strcmp("STDIN", filename) ) {
1178 fp = stdin;
1179 } else {
1180 fp = fopen( filename, "r" );
1181 if( NULL == fp ) {
1182 if( !args->quiet ) fprintf( stderr, "Error opening %s\n", filename );
1183 retval = -3;
1184 goto error;
1185 }
1186 }
1187
1188 buin.info.data_end = fread(buin.data, 1, buin.info.total_size, fp);
1189 if( buin.info.data_end == 0 ) {
1190 if( !args->quiet ) fprintf( stderr, "ERROR: no bytes read\n" );
1191 retval = -4;
1192 goto error;
1193 }
1194
1195 if( !args->quiet )
1196 fprintf( stderr, "Read 0x%X bytes, making hex with address offset 0x%X.\n",
1197 buin.info.data_end + 1, target_offset );
1198
1199 retval = intel_hex_from_buffer( &buin, args->com_convert_data.force, target_offset );
1200
1201 error:
1202 if( NULL != buin.data ) {
1203 free( buin.data );
1204 buin.data = NULL;
1205 }
1206
1207 return retval;
1208 }
1209
1210
1211 int32_t parse_arguments( struct programmer_arguments *args,
1212 const size_t argc,
1213 char **argv )
1214 {
1215 int32_t i;
1216 int32_t status = 0;
1217
1218 if( NULL == args )
1219 return -1;
1220
1221 /* initialize the argument block to empty, known values */
1222 args->target = tar_none;
1223 args->command = com_none;
1224 args->quiet = 0;
1225 args->suppressBootloader = 0;
1226
1227 /* Special case - check for the help commands which do not require a device type */
1228 if( argc == 2 ) {
1229 if( 0 == strcasecmp(argv[1], "--version") ) {
1230 fprintf( stderr, PACKAGE_STRING "\n");
1231 return 1;
1232 }
1233 if( 0 == strcasecmp(argv[1], "--targets") ) {
1234 list_targets( LIST_STD );
1235 return 1;
1236 }
1237 if( 0 == strcasecmp(argv[1], "--targets-tex") ) {
1238 list_targets( LIST_TEX );
1239 return 1;
1240 }
1241 if( 0 == strcasecmp(argv[1], "--targets-html") ) {
1242 list_targets( LIST_HTML );
1243 return 1;
1244 }
1245 if( 0 == strcasecmp(argv[1], "--help") ||
1246 0 == strcasecmp(argv[1], "-h") ||
1247 0 == strcasecmp(argv[1], "--h") ) {
1248 usage();
1249 return 1;
1250 }
1251 }
1252
1253 /* Make sure there are the minimum arguments */
1254 if( argc < 3 ) {
1255 basic_help();
1256 return -1;
1257 }
1258
1259 if( 0 != assign_target(args, argv[1], target_map) ) {
1260 fprintf( stderr, "Unsupported target '%s'.\n", argv[1]);
1261 status = -3;
1262 goto done;
1263 }
1264
1265 if( 0 != assign_option((int32_t *) &(args->command), argv[2], command_map) ) {
1266 status = -4;
1267 goto done;
1268 }
1269
1270 /* These were taken care of above. */
1271 *argv[0] = '\0';
1272 *argv[1] = '\0';
1273 *argv[2] = '\0';
1274
1275 /* assign command specific default values */
1276 switch( args->command ) {
1277 case com_flash :
1278 args->com_flash_data.force = 0;
1279 args->com_flash_data.segment = mem_flash;
1280 break;
1281 case com_launch :
1282 args->com_launch_config.noreset = 0;
1283 break;
1284 case com_dump :
1285 args->com_read_data.segment = mem_flash;
1286 args->com_flash_data.force = 0;
1287 break;
1288 case com_bin2hex :
1289 args->com_convert_data.segment = mem_flash;
1290 break;
1291 default :
1292 break;
1293 }
1294
1295 if( 0 != assign_global_options(args, argc, argv) ) {
1296 status = -5;
1297 goto done;
1298 }
1299
1300 if( 0 != assign_command_options(args, argc, argv) ) {
1301 status = -6;
1302 goto done;
1303 }
1304
1305 /* Make sure there weren't any *extra* options. */
1306 for( i = 0; i < argc; i++ ) {
1307 if( '\0' != *argv[i] ) {
1308 fprintf( stderr, "unrecognized parameter\n" );
1309 status = -7;
1310 goto done;
1311 }
1312 }
1313
1314 /* if this is a flash command, restore the filename */
1315 if( (com_flash == args->command) || (com_eflash == args->command)
1316 || (com_user == args->command) ) {
1317 if( 0 == args->com_flash_data.file ) {
1318 // TODO : it should be ok to not have a filename if --serial=hexdigits:offset is
1319 // provided, this should be implemented.. in fact, given that most of this
1320 // program is written to use a single command by it self, this probably should
1321 // be separated out as a new command. The caveat is if data is written to '\0'
1322 // in the hex file, serialize will do nothing bc can't un-write w/o erase
1323 fprintf( stderr, "flash filename is missing\n" );
1324 status = -8;
1325 goto done;
1326 }
1327 args->com_flash_data.file[0] = args->com_flash_data.original_first_char;
1328 }
1329
1330 if( (com_bin2hex == args->command) || (com_hex2bin == args->command) ) {
1331 if( 0 == args->com_convert_data.file ) {
1332 fprintf( stderr, "conversion filename is missing\n" );
1333 status = -8;
1334 goto done;
1335 }
1336 args->com_convert_data.file[0] = args->com_convert_data.original_first_char;
1337
1338 if ( com_bin2hex == args->command ) {
1339 return execute_bin2hex( args );
1340 } else {
1341 return execute_hex2bin( args );
1342 }
1343 }
1344
1345 done:
1346 if( 1 < debug ) {
1347 print_args( args );
1348 }
1349
1350 if(-3 == status ) {
1351 list_targets( LIST_STD );
1352 } else if( 0 != status ) {
1353 usage();
1354 }
1355
1356 return status;
1357 }