2 * dfu.c -- DFU back-end routines
4 * Copyright (C) 2012 Samsung Electronics
5 * author: Lukasz Majewski <l.majewski@samsung.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <linux/list.h>
28 #include <linux/compiler.h>
30 static LIST_HEAD(dfu_list);
31 static int dfu_alt_num;
33 static int dfu_find_alt_num(const char *s)
44 static unsigned char __aligned(CONFIG_SYS_CACHELINE_SIZE)
45 dfu_buf[DFU_DATA_BUF_SIZE];
47 int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
49 static unsigned char *i_buf;
50 static int i_blk_seq_num;
54 debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x i_buf: 0x%p\n",
55 __func__, dfu->name, buf, size, blk_seq_num, i_buf);
57 if (blk_seq_num == 0) {
62 if (i_blk_seq_num++ != blk_seq_num) {
63 printf("%s: Wrong sequence number! [%d] [%d]\n",
64 __func__, i_blk_seq_num, blk_seq_num);
68 memcpy(i_buf, buf, size);
72 /* Integrity check (if needed) */
73 debug("%s: %s %d [B] CRC32: 0x%x\n", __func__, dfu->name,
74 i_buf - dfu_buf, crc32(0, dfu_buf, i_buf - dfu_buf));
76 w_size = i_buf - dfu_buf;
77 ret = dfu->write_medium(dfu, dfu_buf, &w_size);
79 debug("%s: Write error!\n", __func__);
89 int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
91 static unsigned char *i_buf;
92 static int i_blk_seq_num;
97 debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x i_buf: 0x%p\n",
98 __func__, dfu->name, buf, size, blk_seq_num, i_buf);
100 if (blk_seq_num == 0) {
102 ret = dfu->read_medium(dfu, i_buf, &r_size);
103 debug("%s: %s %ld [B]\n", __func__, dfu->name, r_size);
105 /* Integrity check (if needed) */
106 crc = crc32(0, dfu_buf, r_size);
109 if (i_blk_seq_num++ != blk_seq_num) {
110 printf("%s: Wrong sequence number! [%d] [%d]\n",
111 __func__, i_blk_seq_num, blk_seq_num);
115 if (r_size >= size) {
116 memcpy(buf, i_buf, size);
121 memcpy(buf, i_buf, r_size);
123 debug("%s: %s CRC32: 0x%x\n", __func__, dfu->name, crc);
124 puts("UPLOAD ... done\nCtrl+C to exit ...\n");
134 static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt,
135 char *interface, int num)
139 debug("%s: %s interface: %s num: %d\n", __func__, s, interface, num);
140 st = strsep(&s, " ");
141 strcpy(dfu->name, st);
146 /* Specific for mmc device */
147 if (strcmp(interface, "mmc") == 0) {
148 if (dfu_fill_entity_mmc(dfu, s))
151 printf("%s: Device %s not (yet) supported!\n",
152 __func__, interface);
159 void dfu_free_entities(void)
161 struct dfu_entity *dfu, *p, *t = NULL;
163 list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) {
164 list_del(&dfu->list);
169 INIT_LIST_HEAD(&dfu_list);
172 int dfu_config_entities(char *env, char *interface, int num)
174 struct dfu_entity *dfu;
178 dfu_alt_num = dfu_find_alt_num(env);
179 debug("%s: dfu_alt_num=%d\n", __func__, dfu_alt_num);
181 dfu = calloc(sizeof(*dfu), dfu_alt_num);
184 for (i = 0; i < dfu_alt_num; i++) {
186 s = strsep(&env, ";");
187 ret = dfu_fill_entity(&dfu[i], s, i, interface, num);
191 list_add_tail(&dfu[i].list, &dfu_list);
197 const char *dfu_get_dev_type(enum dfu_device_type t)
199 const char *dev_t[] = {NULL, "eMMC", "OneNAND", "NAND" };
203 const char *dfu_get_layout(enum dfu_layout l)
205 const char *dfu_layout[] = {NULL, "RAW_ADDR", "FAT", "EXT2",
207 return dfu_layout[l];
210 void dfu_show_entities(void)
212 struct dfu_entity *dfu;
214 puts("DFU alt settings list:\n");
216 list_for_each_entry(dfu, &dfu_list, list) {
217 printf("dev: %s alt: %d name: %s layout: %s\n",
218 dfu_get_dev_type(dfu->dev_type), dfu->alt,
219 dfu->name, dfu_get_layout(dfu->layout));
223 int dfu_get_alt_number(void)
228 struct dfu_entity *dfu_get_entity(int alt)
230 struct dfu_entity *dfu;
232 list_for_each_entry(dfu, &dfu_list, list) {