1 /*
2  *  Simple quirk system for dfu-util
3  *
4  *  Copyright 2010-2017 Tormod Volden
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24 
25 #include <stdint.h>
26 #include <stdio.h>
27 #include <string.h>
28 
29 #include "portable.h"
30 #include "quirks.h"
31 #include "dfuse_mem.h"
32 
get_quirks(uint16_t vendor,uint16_t product,uint16_t bcdDevice)33 uint16_t get_quirks(uint16_t vendor, uint16_t product, uint16_t bcdDevice)
34 {
35 	uint16_t quirks = 0;
36 
37 	/* Device returns bogus bwPollTimeout values */
38 	if ((vendor == VENDOR_OPENMOKO || vendor == VENDOR_FIC) &&
39 	    product >= PRODUCT_FREERUNNER_FIRST &&
40 	    product <= PRODUCT_FREERUNNER_LAST)
41 		quirks |= QUIRK_POLLTIMEOUT;
42 
43 	if (vendor == VENDOR_VOTI &&
44 	    (product == PRODUCT_OPENPCD || product == PRODUCT_SIMTRACE ||
45 	     product == PRODUCT_OPENPICC))
46 		quirks |= QUIRK_POLLTIMEOUT;
47 
48 	/* Reports wrong DFU version in DFU descriptor */
49 	if (vendor == VENDOR_LEAFLABS &&
50 	    product == PRODUCT_MAPLE3 &&
51 	    bcdDevice == 0x0200)
52 		quirks |= QUIRK_FORCE_DFU11;
53 
54 	/* old devices(bcdDevice == 0) return bogus bwPollTimeout values */
55 	if (vendor == VENDOR_SIEMENS &&
56 	    (product == PRODUCT_PXM40 || product == PRODUCT_PXM50) &&
57 	    bcdDevice == 0)
58 		quirks |= QUIRK_POLLTIMEOUT;
59 
60 	/* M-Audio Transit returns bogus bwPollTimeout values */
61 	if (vendor == VENDOR_MIDIMAN &&
62 	    product == PRODUCT_TRANSIT)
63 		quirks |= QUIRK_POLLTIMEOUT;
64 
65 	/* Some GigaDevice GD32 devices have improperly-encoded serial numbers
66 	 * and bad DfuSe descriptors which we use serial number to correct.
67 	 * They also "leave" without a DFU_GETSTATUS request */
68 	if (vendor == VENDOR_GIGADEVICE &&
69 	    product == PRODUCT_GD32) {
70 		quirks |= QUIRK_UTF8_SERIAL;
71 		quirks |= QUIRK_DFUSE_LAYOUT;
72 		quirks |= QUIRK_DFUSE_LEAVE;
73 	}
74 
75 	return (quirks);
76 }
77 
78 #define GD32VF103_FLASH_BASE 0x08000000
79 
fixup_dfuse_layout(struct dfu_if * dif,struct memsegment ** segment_list)80 void fixup_dfuse_layout(struct dfu_if *dif, struct memsegment **segment_list)
81 {
82 	if (dif->vendor == VENDOR_GIGADEVICE &&
83 	    dif->product == PRODUCT_GD32 &&
84 	    dif->altsetting == 0 &&
85 	    dif->serial_name &&
86 	    strlen(dif->serial_name) == 4 &&
87 	    dif->serial_name[0] == '3' &&
88 	    dif->serial_name[3] == 'J') {
89 		struct memsegment *seg;
90 		int count;
91 
92 		printf("Found GD32VF103, which reports a bad page size and "
93 		       "count for its internal memory.\n");
94 
95 		seg = find_segment(*segment_list, GD32VF103_FLASH_BASE);
96 		if (!seg) {
97 			warnx("Could not fix GD32VF103 layout because there "
98 			      "is no segment at 0x%08x", GD32VF103_FLASH_BASE);
99 			return;
100 		}
101 
102 		/* All GD32VF103 have this page size, according to Nucleisys's
103 		 * dfu-util (https://github.com/riscv-mcu/gd32-dfu-utils/). */
104 		seg->pagesize = 1024;
105 
106 		/* From Tables 2-1 and 2-2 ("devices features and peripheral
107 		 * list") in the GD32VF103 Datasheet */
108 		if (dif->serial_name[2] == 'B') {
109 			count = 128;
110 		} else if (dif->serial_name[2] == '8') {
111 			count = 64;
112 		} else if (dif->serial_name[2] == '6') {
113 			count = 32;
114 		} else if (dif->serial_name[2] == '4') {
115 			count = 16;
116 		} else {
117 			warnx("Unknown flash size '%c' in part number; "
118 			      "defaulting to 128KB.", dif->serial_name[2]);
119 			count = 128;
120 		}
121 
122 		seg->end = seg->start + (count * seg->pagesize) - 1;
123 
124 		printf("Fixed layout based on part number: page size %d, "
125 		       "count %d.\n", seg->pagesize, count);
126 	}
127 }
128