Root/usbboot/src/cmd.c

1/*
2 * Copyright(C) 2009 Qi Hardware Inc.,
3 * Authors: Marek Lindner <lindner_marek@yahoo.de>
4 * Xiangfu Liu <xiangfu@sharism.cc>
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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <stdio.h>
21#include <string.h>
22#include <unistd.h>
23#include <stdlib.h>
24#include <errno.h>
25#include <fcntl.h>
26#include <sys/types.h>
27#include <sys/stat.h>
28#include <ctype.h>
29#include "cmd.h"
30#include "ingenic_cfg.h"
31#include "ingenic_usb.h"
32#include "usb_boot_defines.h"
33
34extern int com_argc;
35extern char com_argv[MAX_ARGC][MAX_COMMAND_LENGTH];
36extern char * stage1;
37
38struct ingenic_dev ingenic_dev;
39struct hand hand;
40struct sdram_in sdram_in;
41struct nand_in nand_in;
42static struct nand_out nand_out;
43
44unsigned int total_size;
45unsigned char code_buf[4 * 512 * 1024];
46unsigned char check_buf[4 * 512 * 1024];
47unsigned char cs[16];
48unsigned char ret[8];
49
50static const char IMAGE_TYPE[][30] = {
51    "with oob and ecc",
52    "with oob and without ecc",
53    "without oob",
54};
55
56static int load_file(struct ingenic_dev *ingenic_dev, const char *file_path)
57{
58    struct stat fstat;
59    int fd, status, res = -1;
60
61    status = stat(file_path, &fstat);
62
63    if (status < 0) {
64        fprintf(stderr, "Error - can't get file size from '%s': %s\n",
65            file_path, strerror(errno));
66        goto out;
67    }
68
69    ingenic_dev->file_len = fstat.st_size;
70    ingenic_dev->file_buff = code_buf;
71
72    fd = open(file_path, O_RDONLY);
73
74    if (fd < 0) {
75        fprintf(stderr, "Error - can't open file '%s': %s\n",
76            file_path, strerror(errno));
77        goto out;
78    }
79
80    status = read(fd, ingenic_dev->file_buff, ingenic_dev->file_len);
81
82    if (status < ingenic_dev->file_len) {
83        fprintf(stderr, "Error - can't read file '%s': %s\n",
84            file_path, strerror(errno));
85        goto close;
86    }
87
88    /* write args to code */
89    memcpy(ingenic_dev->file_buff + 8, &hand.fw_args,
90           sizeof(struct fw_args));
91
92    res = 1;
93
94close:
95    close(fd);
96out:
97    return res;
98}
99
100/* after upload stage2. must init device */
101void init_cfg()
102{
103    if (usb_get_ingenic_cpu(&ingenic_dev) < 3) {
104        printf(" XBurst CPU not booted yet, boot it first!\n");
105        return;
106    }
107
108    ingenic_dev.file_buff = &hand;
109    ingenic_dev.file_len = sizeof(hand);
110    if (usb_send_data_to_ingenic(&ingenic_dev) != 1)
111        goto xout;
112
113    sleep(1);
114    if (usb_ingenic_configration(&ingenic_dev, DS_hand) != 1)
115        goto xout;
116
117    if (usb_read_data_from_ingenic(&ingenic_dev, ret, 8) != 1)
118        goto xout;
119
120    printf(" Configuring XBurst CPU succeeded.\n");
121    return;
122xout:
123    printf(" Configuring XBurst CPU failed.\n");
124}
125
126int boot(char *stage1_path, char *stage2_path){
127    int status;
128
129    status = usb_get_ingenic_cpu(&ingenic_dev);
130    switch (status) {
131    case JZ4740V1:
132        status = 0;
133        hand.fw_args.cpu_id = 0x4740;
134        break;
135    case JZ4750V1:
136        status = 0;
137        hand.fw_args.cpu_id = 0x4750;
138        break;
139    case JZ4760V1:
140        status = 0;
141        hand.fw_args.cpu_id = 0x4760;
142        break;
143    case BOOT4740:
144        status = 1;
145        hand.fw_args.cpu_id = 0x4740;
146        break;
147    case BOOT4750:
148        status = 1;
149        hand.fw_args.cpu_id = 0x4750;
150        break;
151    case BOOT4760:
152        status = 1;
153        hand.fw_args.cpu_id = 0x4760;
154        break;
155    default:
156        return 1;
157    }
158
159    if (status) {
160        printf(" Already booted.\n");
161        return 1;
162    } else {
163        printf(" CPU not yet booted, now booting...\n");
164
165        /* now we upload the boot stage1 */
166        printf(" Loading stage1 from '%s'\n", stage1_path);
167        if (load_file(&ingenic_dev, stage1_path) < 1)
168            return -1;
169
170        if (usb_ingenic_upload(&ingenic_dev, 1) < 1)
171            return -1;
172
173        /* now we upload the boot stage2 */
174        usleep(100);
175        printf(" Loading stage2 from '%s'\n", stage2_path);
176        if (load_file(&ingenic_dev, stage2_path) < 1)
177            return -1;
178
179        if (usb_ingenic_upload(&ingenic_dev, 2) < 1)
180            return -1;
181
182        printf(" Booted successfully!\n");
183    }
184    usleep(100);
185    init_cfg();
186    return 1;
187}
188
189/* nand function */
190int error_check(unsigned char *org,unsigned char * obj,unsigned int size)
191{
192    unsigned int i;
193    printf(" Comparing %d bytes - ", size);
194    for (i = 0; i < size; i++) {
195        if (org[i] != obj[i]) {
196            unsigned int s = (i < 8) ? i : i - 8; // start_dump
197            printf("FAIL at off %d, wrote 0x%x, read 0x%x\n", i, org[i], obj[i]);
198            printf(" off %d write: %02x %02x %02x %02x %02x %02x %02x %02x"
199                   " %02x %02x %02x %02x %02x %02x %02x %02x\n", s,
200                org[s], org[s+1], org[s+2], org[s+3], org[s+4], org[s+5], org[s+6], org[s+7],
201                org[s+8], org[s+9], org[s+10], org[s+11], org[s+12], org[s+13], org[s+14], org[s+15]);
202            printf(" off %d read: %02x %02x %02x %02x %02x %02x %02x %02x"
203                   " %02x %02x %02x %02x %02x %02x %02x %02x\n", s,
204                obj[s], obj[s+1], obj[s+2], obj[s+3], obj[s+4], obj[s+5], obj[s+6], obj[s+7],
205                obj[s+8], obj[s+9], obj[s+10], obj[s+11], obj[s+12], obj[s+13], obj[s+14], obj[s+15]);
206            return 0;
207        }
208    }
209    printf("SUCCESS\n");
210    return 1;
211}
212
213int nand_markbad(struct nand_in *nand_in)
214{
215    if (usb_get_ingenic_cpu(&ingenic_dev) < 3) {
216        printf(" Device unboot! Boot it first!\n");
217        return -1;
218    }
219    printf(" mark bad block : %d\n",nand_in->start);
220    usb_send_data_address_to_ingenic(&ingenic_dev, nand_in->start);
221    usb_ingenic_nand_ops(&ingenic_dev, NAND_MARK_BAD);
222    usb_read_data_from_ingenic(&ingenic_dev, ret, 8);
223    printf(" Mark bad block at %d\n",((ret[3] << 24) |
224                       (ret[2] << 16) |
225                       (ret[1] << 8) |
226                       (ret[0] << 0)) / hand.nand_ppb);
227    return 0;
228}
229
230int nand_program_check(struct nand_in *nand_in,
231               struct nand_out *nand_out,
232               unsigned int *start_page)
233{
234    unsigned int i, page_num, cur_page = -1;
235    unsigned int start_addr;
236    unsigned short temp;
237    int status = -1;
238
239    printf(" Writing NAND page %d len %d...\n", nand_in->start, nand_in->length);
240    if (nand_in->length > (unsigned int)MAX_TRANSFER_SIZE) {
241        printf(" Buffer size too long!\n");
242        goto err;
243    }
244
245#ifdef CONFIG_NAND_OUT
246    unsigned char status_buf[32];
247    nand_out->status = status_buf;
248    for (i = 0; i < nand_in->max_chip; i++)
249        (nand_out->status)[i] = 0; /* set all status to fail */
250#endif
251
252    int cpu = usb_get_ingenic_cpu(&ingenic_dev);
253    if (cpu != BOOT4740 && cpu != BOOT4750 && cpu != BOOT4760) {
254        printf(" Device unboot! Boot it first!\n");
255        goto err;
256    }
257
258    ingenic_dev.file_buff = nand_in->buf;
259    ingenic_dev.file_len = nand_in->length;
260    usb_send_data_to_ingenic(&ingenic_dev);
261    for (i = 0; i < nand_in->max_chip; i++) {
262        if ((nand_in->cs_map)[i] == 0)
263            continue;
264        if (nand_in->option == NO_OOB) {
265            page_num = nand_in->length / hand.nand_ps;
266            if ((nand_in->length % hand.nand_ps) !=0)
267                page_num++;
268        } else {
269            page_num = nand_in->length /
270                (hand.nand_ps + hand.nand_os);
271            if ((nand_in->length% (hand.nand_ps + hand.nand_os)) !=0)
272                page_num++;
273        }
274        temp = ((nand_in->option << 12) & 0xf000) +
275            ((i<<4) & 0xff0) + NAND_PROGRAM;
276        if (usb_send_data_address_to_ingenic(&ingenic_dev, nand_in->start) != 1)
277            goto err;
278        if (usb_send_data_length_to_ingenic(&ingenic_dev, page_num) != 1)
279            goto err;
280        if (usb_ingenic_nand_ops(&ingenic_dev, temp) != 1)
281            goto err;
282        if (usb_read_data_from_ingenic(&ingenic_dev, ret, 8) != 1)
283            goto err;
284
285        printf(" Finish! (len %d start_page %d page_num %d)\n",
286               nand_in->length, nand_in->start, page_num);
287
288        /* Read back to check! */
289        usb_send_data_address_to_ingenic(&ingenic_dev, nand_in->start);
290        usb_send_data_length_to_ingenic(&ingenic_dev, page_num);
291
292        switch (nand_in->option) {
293        case OOB_ECC:
294            temp = ((OOB_ECC << 12) & 0xf000) +
295                ((i << 4) & 0xff0) + NAND_READ;
296            start_addr = page_num * (hand.nand_ps + hand.nand_os);
297            break;
298        case OOB_NO_ECC: /* do not support data verify */
299            temp = ((OOB_NO_ECC << 12) & 0xf000) +
300                ((i << 4) & 0xff0) + NAND_READ;
301            start_addr = page_num * (hand.nand_ps + hand.nand_os);
302            break;
303        case NO_OOB:
304            temp = ((NO_OOB << 12) & 0xf000) +
305                ((i << 4) & 0xff0) + NAND_READ;
306            start_addr = page_num * hand.nand_ps;
307            break;
308        default:
309            ;
310        }
311
312        printf(" Checking %d bytes...", nand_in->length);
313        usb_ingenic_nand_ops(&ingenic_dev, temp);
314        usb_read_data_from_ingenic(&ingenic_dev, check_buf, start_addr);
315        usb_read_data_from_ingenic(&ingenic_dev, ret, 8);
316
317        cur_page = (ret[3] << 24) | (ret[2] << 16) | (ret[1] << 8) |
318            (ret[0] << 0);
319
320#ifdef CONFIG_NAND_OUT
321        (nand_out->status)[i] = 1;
322#endif
323
324        if (nand_in->start < 1 &&
325            hand.nand_ps == 4096 &&
326            hand.fw_args.cpu_id == 0x4740) {
327            printf(" no check! End at Page: %d\n", cur_page);
328            continue;
329        }
330
331        if (!nand_in->check(nand_in->buf, check_buf, nand_in->length)) {
332#ifdef CONFIG_NAND_OUT
333            (nand_out->status)[i] = 0;
334#endif
335            struct nand_in bad;
336            // tbd: doesn't the other side skip bad blocks too? Can we just deduct 1 from cur_page?
337            // tbd: why do we only mark a block as bad if the last page in the block was written?
338            bad.start = (cur_page - 1) / hand.nand_ppb;
339            if (cur_page % hand.nand_ppb == 0)
340                nand_markbad(&bad);
341        }
342
343        printf(" End at Page: %d\n", cur_page);
344    }
345
346    *start_page = cur_page;
347
348    status = 1;
349err:
350    return status;
351}
352
353int nand_erase(struct nand_in *nand_in)
354{
355    unsigned int start_blk, blk_num, end_block;
356    int i;
357
358    start_blk = nand_in->start;
359    blk_num = nand_in->length;
360    if (start_blk > (unsigned int)NAND_MAX_BLK_NUM) {
361        printf(" Start block number overflow!\n");
362        return -1;
363    }
364    if (blk_num > (unsigned int)NAND_MAX_BLK_NUM) {
365        printf(" Length block number overflow!\n");
366        return -1;
367    }
368
369    if (usb_get_ingenic_cpu(&ingenic_dev) < 3) {
370        printf(" Device unboot! Boot it first!\n");
371        return -1;
372    }
373
374    for (i = 0; i < nand_in->max_chip; i++) {
375        if ((nand_in->cs_map)[i]==0)
376            continue;
377        printf(" Erasing No.%d device No.%d flash (start_blk %u blk_num %u)......\n",
378               nand_in->dev, i, start_blk, blk_num);
379
380        usb_send_data_address_to_ingenic(&ingenic_dev, start_blk);
381        usb_send_data_length_to_ingenic(&ingenic_dev, blk_num);
382
383        unsigned short temp = ((i << 4) & 0xff0) + NAND_ERASE;
384        usb_ingenic_nand_ops(&ingenic_dev, temp);
385
386        usb_read_data_from_ingenic(&ingenic_dev, ret, 8);
387        printf(" Finish!");
388    }
389    end_block = ((ret[3] << 24) |
390             (ret[2] << 16) |
391             (ret[1] << 8) |
392             (ret[0] << 0)) / hand.nand_ppb;
393    printf(" Return: %02x %02x %02x %02x %02x %02x %02x %02x (position %d)\n",
394           ret[0], ret[1], ret[2], ret[3], ret[4], ret[5], ret[6], ret[7], end_block);
395    if (!hand.nand_force_erase) {
396    /* not force erase, show bad block infomation */
397        printf(" There are marked bad blocks: %d\n",
398               end_block - start_blk - blk_num );
399    } else {
400    /* force erase, no bad block infomation can show */
401        printf(" Force erase, no bad block infomation!\n" );
402    }
403
404    return 1;
405}
406
407int nand_program_file(struct nand_in *nand_in,
408              struct nand_out *nand_out,
409              char *fname)
410{
411
412    int flen, m, j, k;
413    unsigned int start_page = 0, page_num, code_len, offset, transfer_size;
414    int fd, status;
415    struct stat fstat;
416    struct nand_in n_in;
417    struct nand_out n_out;
418
419#ifdef CONFIG_NAND_OUT
420    unsigned char status_buf[32];
421    nand_out->status = status_buf;
422    for (i=0; i<nand_in->max_chip; i++)
423        (nand_out->status)[i] = 0; /* set all status to fail */
424#endif
425    status = stat(fname, &fstat);
426
427    if (status < 0) {
428        fprintf(stderr, "Error - can't get file size from '%s': %s\n",
429            fname, strerror(errno));
430        return -1;
431    }
432    flen = fstat.st_size;
433
434    fd = open(fname, O_RDONLY);
435    if (fd < 0) {
436        fprintf(stderr, "Error - can't open file '%s': %s\n",
437            fname, strerror(errno));
438        return -1;
439    }
440
441    printf(" Programing No.%d device, flen %d, start page %d...\n",nand_in->dev, flen, nand_in->start);
442    n_in.start = nand_in->start / hand.nand_ppb;
443    if (nand_in->option == NO_OOB) {
444        if (flen % (hand.nand_ppb * hand.nand_ps) == 0)
445            n_in.length = flen / (hand.nand_ps * hand.nand_ppb);
446        else
447            n_in.length = flen / (hand.nand_ps * hand.nand_ppb) + 1;
448    } else {
449        if (flen % (hand.nand_ppb * (hand.nand_ps + hand.nand_os)) == 0)
450            n_in.length = flen /
451                ((hand.nand_ps + hand.nand_os) * hand.nand_ppb);
452        else
453            n_in.length = flen /
454                ((hand.nand_ps + hand.nand_os) * hand.nand_ppb)
455                + 1;
456    }
457    n_in.cs_map = nand_in->cs_map;
458    n_in.dev = nand_in->dev;
459    n_in.max_chip = nand_in->max_chip;
460    if (nand_erase(&n_in) != 1)
461        return -1;
462    if (nand_in->option == NO_OOB)
463        transfer_size = (hand.nand_ppb * hand.nand_ps);
464    else
465        transfer_size = (hand.nand_ppb * (hand.nand_ps + hand.nand_os));
466
467    m = flen / transfer_size;
468    j = flen % transfer_size;
469    printf(" Size to send %d, transfer_size %d\n", flen, transfer_size);
470    printf(" Image type : %s\n", IMAGE_TYPE[nand_in->option]);
471    printf(" It will cause %d times buffer transfer.\n", j == 0 ? m : m + 1);
472
473#ifdef CONFIG_NAND_OUT
474    for (i = 0; i < nand_in->max_chip; i++)
475        (nand_out->status)[i] = 1; /* set all status to success! */
476#endif
477
478    offset = 0;
479    for (k = 0; k < m; k++) {
480        if (nand_in->option == NO_OOB)
481            page_num = transfer_size / hand.nand_ps;
482        else
483            page_num = transfer_size / (hand.nand_ps + hand.nand_os);
484
485        code_len = transfer_size;
486        status = read(fd, code_buf, code_len);
487        if (status < code_len) {
488            fprintf(stderr, "Error - can't read file '%s': %s\n",
489                fname, strerror(errno));
490            return -1;
491        }
492
493        nand_in->length = code_len; /* code length,not page number! */
494        nand_in->buf = code_buf;
495        if (nand_program_check(nand_in, &n_out, &start_page) == -1)
496            return -1;
497
498        if (start_page - nand_in->start > hand.nand_ppb)
499            printf(" Info - skip bad block!\n");
500        nand_in->start = start_page;
501
502#ifdef CONFIG_NAND_OUT
503        for (i = 0; i < nand_in->max_chip; i++) {
504            (nand_out->status)[i] = (nand_out->status)[i] *
505                (n_out.status)[i];
506        }
507#endif
508        offset += code_len ;
509    }
510
511    if (j) {
512        code_len = j;
513        if (j % hand.nand_ps)
514            j += hand.nand_ps - (j % hand.nand_ps);
515        memset(code_buf, 0, j); /* set all to null */
516
517        status = read(fd, code_buf, code_len);
518
519        if (status < code_len) {
520            fprintf(stderr, "Error - can't read file '%s': %s\n",
521                fname, strerror(errno));
522            return -1;
523        }
524
525        nand_in->length = j;
526        nand_in->buf = code_buf;
527        if (nand_program_check(nand_in, &n_out, &start_page) == -1)
528            return -1;
529
530        if (start_page - nand_in->start > hand.nand_ppb)
531            printf(" Info - skip bad block!");
532
533#ifdef CONFIG_NAND_OUT
534        for (i=0; i < nand_in->max_chip; i++) {
535            (nand_out->status)[i] = (nand_out->status)[i] *
536                (n_out.status)[i];
537        }
538#endif
539    }
540    
541    close(fd);
542    return 1;
543}
544
545int nand_program_file_planes(struct nand_in *nand_in,
546              struct nand_out *nand_out,
547              char *fname)
548{
549    printf(" not implement yet !\n");
550    return -1;
551}
552
553int init_nand_in(void)
554{
555    nand_in.buf = code_buf;
556    nand_in.check = error_check;
557    nand_in.dev = 0;
558    nand_in.cs_map = cs;
559    memset(nand_in.cs_map, 0, MAX_DEV_NUM);
560
561    nand_in.max_chip = 16;
562    return 0;
563}
564
565int nand_prog(void)
566{
567    int status = -1;
568    char *image_file;
569    char *help = " Usage: nprog (1) (2) (3) (4) (5)\n"
570        " (1)\tstart page number\n"
571        " (2)\timage file name\n"
572        " (3)\tdevice index number\n"
573        " (4)\tflash index number\n"
574        " (5) image type must be:\n"
575        " \t-n:\tno oob\n"
576        " \t-o:\twith oob no ecc\n"
577        " \t-e:\twith oob and ecc\n";
578
579    if (com_argc != 6) {
580        printf("%s", help);
581        return 0;
582    }
583
584    init_nand_in();
585
586    nand_in.start = atoi(com_argv[1]);
587    image_file = com_argv[2];
588    nand_in.dev = atoi(com_argv[3]);
589
590    (nand_in.cs_map)[atoi(com_argv[4])] = 1;
591    if (!strcmp(com_argv[5], "-e"))
592        nand_in.option = OOB_ECC;
593    else if (!strcmp(com_argv[5], "-o"))
594        nand_in.option = OOB_NO_ECC;
595    else if (!strcmp(com_argv[5], "-n"))
596        nand_in.option = NO_OOB;
597    else
598        printf("%s", help);
599
600    if (hand.nand_plane > 1)
601        nand_program_file_planes(&nand_in, &nand_out, image_file);
602    else
603        nand_program_file(&nand_in, &nand_out, image_file);
604
605#ifdef CONFIG_NAND_OUT
606    printf(" Flash check result:\n");
607    int i;
608    for (i = 0; i < 16; i++)
609        printf(" %d", (nand_out.status)[i]);
610#endif
611
612    status = 1;
613err:
614    return status;
615}
616
617int nand_query(void)
618{
619    int i;
620    unsigned char csn;
621
622    if (com_argc < 3) {
623        printf(" Usage: nquery (1) (2)\n"
624               " (1):device index number\n"
625               " (2):flash index number\n");
626        return -1;
627    }
628    init_nand_in();
629
630    nand_in.dev = atoi(com_argv[1]);
631    (nand_in.cs_map)[atoi(com_argv[2])] = 1;
632
633    for (i = 0; i < nand_in.max_chip; i++) {
634        if ((nand_in.cs_map)[i] != 0)
635            break;
636    }
637    if (i >= nand_in.max_chip)
638        return -1;
639
640    if (usb_get_ingenic_cpu(&ingenic_dev) < 3) {
641        printf(" Device unboot! Boot it first!\n");
642        return -1;
643    }
644
645    csn = i;
646    printf(" ID of No.%d device No.%d flash: \n", nand_in.dev, csn);
647
648    unsigned short ops = ((csn << 4) & 0xff0) + NAND_QUERY;
649    usb_ingenic_nand_ops(&ingenic_dev, ops);
650    usb_read_data_from_ingenic(&ingenic_dev, ret, 8);
651    printf(" Vendor ID :0x%x \n",(unsigned char)ret[0]);
652    printf(" Product ID :0x%x \n",(unsigned char)ret[1]);
653    printf(" Chip ID :0x%x \n",(unsigned char)ret[2]);
654    printf(" Page ID :0x%x \n",(unsigned char)ret[3]);
655    printf(" Plane ID :0x%x \n",(unsigned char)ret[4]);
656
657    usb_read_data_from_ingenic(&ingenic_dev, ret, 8);
658
659    return 1;
660}
661
662int nand_read(int mode)
663{
664    unsigned int i,j;
665    unsigned int start_addr, length, page_num;
666    unsigned char csn;
667    unsigned short temp = 0;
668    unsigned ram_addr = 0;
669
670    if (com_argc < 5) {
671        printf(" Usage: nread (1) (2) (3) (4)\n"
672               " 1:start page number\n"
673               " 2:length in byte\n"
674               " 3:device index number\n"
675               " 4:flash index number\n"
676               " 5:start SDRAM address\n");
677        return -1;
678    }
679    init_nand_in();
680
681    if (atoi(com_argv[4]) >= MAX_DEV_NUM) {
682        printf(" Flash index number overflow!\n");
683        return -1;
684    }
685    (nand_in.cs_map)[atoi(com_argv[4])] = 1;
686    nand_in.start = atoi(com_argv[1]);
687    nand_in.length= atoi(com_argv[2]);
688    nand_in.dev = atoi(com_argv[3]);
689
690    if (com_argc = 6) {
691        ram_addr = strtoul(com_argv[5], NULL, 0);
692        printf("==%s==", com_argv[5]);
693    }
694    start_addr = nand_in.start;
695    length = nand_in.length;
696
697    if (start_addr > NAND_MAX_PAGE_NUM || length > NAND_MAX_PAGE_NUM ) {
698        printf(" Page number overflow!\n");
699        return -1;
700    }
701    if (usb_get_ingenic_cpu(&ingenic_dev) < 3) {
702        printf(" Device unboot! Boot it first!\n");
703        return -1;
704    }
705    for (i = 0; i < nand_in.max_chip; i++)
706        if ((nand_in.cs_map)[i] != 0)
707            break;
708    if (i >= nand_in.max_chip) return 1;
709    csn = i;
710    printf(" Reading from No.%d device No.%d flash....\n",nand_in.dev,csn);
711
712    page_num = length / hand.nand_ps +1;
713
714    switch(mode) {
715    case NAND_READ:
716        temp = ((NO_OOB<<12) & 0xf000) + ((csn<<4) & 0xff0) + NAND_READ;
717        break;
718    case NAND_READ_OOB:
719        temp = ((csn<<4) & 0xff0) + NAND_READ_OOB;
720        break;
721    case NAND_READ_RAW:
722        temp = ((NO_OOB<<12) & 0xf000) + ((csn<<4) & 0xff0) +
723            NAND_READ_RAW;
724        break;
725    case NAND_READ_TO_RAM:
726        temp = ((NO_OOB<<12) & 0xf000) + ((csn<<4) & 0xff0) +
727            NAND_READ_TO_RAM;
728        printf(" Reading nand to RAM: 0x%x\n", ram_addr);
729        usb_ingenic_start(&ingenic_dev, VR_PROGRAM_START1, ram_addr);
730        break;
731    default:
732        printf(" unknow mode!\n");
733        return -1;
734    }
735
736    usb_send_data_address_to_ingenic(&ingenic_dev, start_addr);
737    usb_send_data_length_to_ingenic(&ingenic_dev, page_num);
738
739    usb_ingenic_nand_ops(&ingenic_dev, temp);
740
741    usb_read_data_from_ingenic(&ingenic_dev, nand_in.buf, page_num * hand.nand_ps);
742
743    for (j = 0; j < length; j++) {
744        if (j % 16 == 0)
745        printf("\n 0x%08x : ",j);
746        printf("%02x ",(nand_in.buf)[j]);
747    }
748    printf("\n");
749
750    usb_read_data_from_ingenic(&ingenic_dev, ret, 8);
751    printf(" Operation end position : %d \n",
752           (ret[3]<<24)|(ret[2]<<16)|(ret[1]<<8)|(ret[0]<<0));
753
754    return 1;
755}
756
757int debug_memory(int obj, unsigned int start, unsigned int size)
758{
759    unsigned int buffer[8],tmp;
760
761    tmp = usb_get_ingenic_cpu(&ingenic_dev);
762    if (tmp > 2) {
763        printf(" This command only run under UNBOOT state!\n");
764        return -1;
765    }
766
767    switch (tmp) {
768    case 1:
769        tmp = 0;
770        hand.fw_args.cpu_id = 0x4740;
771        break;
772    case 2:
773        tmp = 0;
774        hand.fw_args.cpu_id = 0x4750;
775        break;
776    }
777
778    hand.fw_args.debug_ops = 1;/* tell device it's memory debug */
779    hand.fw_args.start = start;
780
781    if (size == 0)
782        hand.fw_args.size = total_size;
783    else
784        hand.fw_args.size = size;
785
786    printf(" Now test memory from 0x%x to 0x%x: \n",
787           start, start + hand.fw_args.size);
788
789    if (load_file(&ingenic_dev, stage1) < 1)
790        return -1;
791    if (usb_ingenic_upload(&ingenic_dev, 1) < 1)
792        return -1;
793
794    usleep(100);
795    usb_read_data_from_ingenic(&ingenic_dev, buffer, 8);
796    if (buffer[0] != 0)
797        printf(" Test memory fail! Last error address is 0x%x !\n",
798               buffer[0]);
799    else
800        printf(" Test memory pass!\n");
801
802    return 1;
803}
804
805int debug_gpio(int obj, unsigned char ops, unsigned char pin)
806{
807    unsigned int tmp;
808
809    tmp = usb_get_ingenic_cpu(&ingenic_dev);
810    if (tmp > 2) {
811        printf(" This command only run under UNBOOT state!\n");
812        return -1;
813    }
814
815    switch (tmp) {
816    case 1:
817        tmp = 0;
818        hand.fw_args.cpu_id = 0x4740;
819        if (pin > 124) {
820            printf(" Jz4740 has 124 GPIO pin in all!\n");
821            return -1;
822        }
823        break;
824    case 2:
825        tmp = 0;
826        hand.fw_args.cpu_id = 0x4750;
827        if (pin > 178) {
828            printf(" Jz4750 has 178 GPIO pin in all!\n");
829            return -1;
830        }
831        break;
832    }
833
834    hand.fw_args.debug_ops = ops;/* tell device it's memory debug */
835    hand.fw_args.pin_num = pin;
836
837    if (ops == 2)
838        printf(" GPIO %d set!\n",pin);
839    else
840        printf(" GPIO %d clear!\n",pin);
841
842    if (load_file(&ingenic_dev, stage1) < 1)
843        return -1;
844    if (usb_ingenic_upload(&ingenic_dev, 1) < 1)
845        return -1;
846
847    return 0;
848}
849
850int debug_go(void)
851{
852    unsigned int addr,obj;
853    if (com_argc<3) {
854        printf(" Usage: go (1) (2) \n"
855               " 1:start SDRAM address\n"
856               " 2:device index number\n");
857        return 0;
858    }
859
860    addr = strtoul(com_argv[1], NULL, 0);
861    obj = atoi(com_argv[2]);
862
863    printf(" Executing No.%d device at address 0x%x\n", obj, addr);
864
865    if (usb_ingenic_start(&ingenic_dev, VR_PROGRAM_START2, addr) < 1)
866        return -1;
867
868    return 1;
869}
870
871int sdram_load(struct sdram_in *sdram_in)
872{
873    if (usb_get_ingenic_cpu(&ingenic_dev) < 3) {
874        printf(" Device unboot! Boot it first!\n");
875        return -1;
876    }
877
878    if (sdram_in->length > (unsigned int) MAX_LOAD_SIZE) {
879        printf(" Image length too long!\n");
880        return -1;
881    }
882
883    ingenic_dev.file_buff = sdram_in->buf;
884    ingenic_dev.file_len = sdram_in->length;
885    usb_send_data_to_ingenic(&ingenic_dev);
886    usb_send_data_address_to_ingenic(&ingenic_dev, sdram_in->start);
887    usb_send_data_length_to_ingenic(&ingenic_dev, sdram_in->length);
888    usb_ingenic_sdram_ops(&ingenic_dev, sdram_in);
889
890    usb_read_data_from_ingenic(&ingenic_dev, ret, 8);
891    printf(" Load last address at 0x%x\n",
892           ((ret[3]<<24)|(ret[2]<<16)|(ret[1]<<8)|(ret[0]<<0)));
893
894    return 1;
895}
896
897int sdram_load_file(struct sdram_in *sdram_in, char *file_path)
898{
899    struct stat fstat;
900    unsigned int flen,m,j,offset,k;
901    int fd, status, res = -1;
902
903    status = stat(file_path, &fstat);
904    if (status < 0) {
905        fprintf(stderr, "Error - can't get file size from '%s': %s\n",
906            file_path, strerror(errno));
907        goto out;
908    }
909    flen = fstat.st_size;
910
911    fd = open(file_path, O_RDONLY);
912    if (fd < 0) {
913        fprintf(stderr, "Error - can't open file '%s': %s\n",
914            file_path, strerror(errno));
915        goto out;
916    }
917
918    m = flen / MAX_LOAD_SIZE;
919    j = flen % MAX_LOAD_SIZE;
920    offset = 0;
921
922    printf(" Total size to send in byte is :%d\n", flen);
923    printf(" Loading data to SDRAM :\n");
924
925    for (k = 0; k < m; k++) {
926        status = read(fd, sdram_in->buf, MAX_LOAD_SIZE);
927        if (status < MAX_LOAD_SIZE) {
928            fprintf(stderr, "Error - can't read file '%s': %s\n",
929                file_path, strerror(errno));
930            goto close;
931        }
932
933        sdram_in->length = MAX_LOAD_SIZE;
934        if (sdram_load(sdram_in) < 1)
935            goto close;
936
937        sdram_in->start += MAX_LOAD_SIZE;
938        if ( k % 60 == 0)
939            printf(" 0x%x \n", sdram_in->start);
940    }
941
942    if (j) {
943        if (j % 4 !=0)
944            j += 4 - (j % 4);
945        status = read(fd, sdram_in->buf, j);
946        if (status < j) {
947            fprintf(stderr, "Error - can't read file '%s': %s\n",
948                file_path, strerror(errno));
949            goto close;
950        }
951
952        sdram_in->length = j;
953        if (sdram_load(sdram_in) < 1)
954            goto close;
955    }
956
957    res = 1;
958
959close:
960    close(fd);
961out:
962    return res;
963}
964
965int device_reset(int ops)
966{
967    if (usb_ingenic_reset(&ingenic_dev, ops) < 1)
968        return -1;
969
970    return 1;
971}
972

Archive Download this file



interactive