comparison loadtools/flprogsrec.c @ 0:e7502631a0f9

initial import from freecalypso-sw rev 1033:5ab737ac3ad7
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 11 Jun 2016 00:13:35 +0000
parents
children 0dd2c87c1b63
comparison
equal deleted inserted replaced
-1:000000000000 0:e7502631a0f9
1 /*
2 * This module implements the flash program-srec and flash program-m0 commands:
3 * programming flash using S-record files as the data source.
4 */
5
6 #include <sys/types.h>
7 #include <stdio.h>
8 #include <stdint.h>
9 #include <stdlib.h>
10 #include "flash.h"
11 #include "srecreader.h"
12
13 extern struct flash_bank_info flash_bank_info[2];
14
15 flashcmd_progsrec_gen(bank, imgfile, is_m0)
16 char *imgfile;
17 {
18 struct flash_bank_info *bi;
19 struct srecreader srr;
20 char *targv[4], shortarg[10], longarg[513];
21 int resp;
22 unsigned long rec_count;
23
24 if (flash_get_cfi(bank) < 0)
25 return(-1);
26 bi = flash_bank_info + bank;
27 srr.filename = imgfile;
28 resp = open_srec_file(&srr);
29 if (resp < 0)
30 return(resp);
31 resp = flash_id_check(bank, 0);
32 if (resp) {
33 fclose(srr.openfile);
34 return(resp);
35 }
36 sprintf(shortarg, "%lx", (u_long) bi->base_addr);
37 targv[0] = bi->ops->loadagent_setbase_cmd;
38 targv[1] = shortarg;
39 targv[2] = 0;
40 printf("Setting flash base address: %s %s\n", targv[0], targv[1]);
41 tpinterf_make_cmd(targv);
42 if (tpinterf_send_cmd() < 0) {
43 fclose(srr.openfile);
44 return(-1);
45 }
46 resp = tpinterf_pass_output(1);
47 if (resp) {
48 fclose(srr.openfile);
49 return(resp);
50 }
51 if (bi->ops->prep_for_program(bi) < 0) {
52 fclose(srr.openfile);
53 return(-1);
54 }
55 targv[0] = bi->ops->loadagent_program_cmd;
56 targv[1] = shortarg;
57 targv[2] = longarg;
58 targv[3] = 0;
59 for (rec_count = 0; ; ) {
60 if (read_s_record(&srr) < 0) {
61 /* error msg already printed */
62 fclose(srr.openfile);
63 return(-1);
64 }
65 if (srr.record_type == '0') {
66 if (srr.lineno == 1)
67 continue;
68 fprintf(stderr,
69 "Warning: S0 record found in line %d of %s (expected in line 1 only)\n",
70 srr.lineno, srr.filename);
71 continue;
72 } else if (srr.record_type == '7')
73 break;
74 else if (srr.record_type != '3') {
75 fprintf(stderr,
76 "Warning: unsupported S%c record type in line %d of %s\n",
77 srr.record_type, srr.lineno, srr.filename);
78 continue;
79 }
80 /* must be S3 */
81 if (s3s7_get_addr_data(&srr) < 0) {
82 /* error msg already printed */
83 fclose(srr.openfile);
84 return(-1);
85 }
86 if (srr.datalen < 1) {
87 fprintf(stderr,
88 "%s line %d: S3 record of zero data length ignored\n",
89 srr.filename, srr.lineno);
90 continue;
91 }
92 if (srr.addr & 1 || srr.datalen & 1) {
93 fprintf(stderr,
94 "%s line %d: violates word alignment requirement\n",
95 srr.filename, srr.lineno);
96 fclose(srr.openfile);
97 return(-1);
98 }
99 srr.addr &= bi->geom->total_size - 1;
100 if (srr.addr + srr.datalen > bi->geom->total_size) {
101 fprintf(stderr,
102 "%s line %d: goes past the end of the flash bank\n",
103 srr.filename, srr.lineno);
104 fclose(srr.openfile);
105 return(-1);
106 }
107 if (!rec_count)
108 printf("Programming flash, each \'.\' is 100 S-records\n");
109 sprintf(shortarg, "%lx", (u_long) srr.addr);
110 build_flashw_hex_string(srr.record + 5, longarg,
111 srr.datalen >> 1, is_m0);
112 tpinterf_make_cmd(targv);
113 if (tpinterf_send_cmd() < 0) {
114 fclose(srr.openfile);
115 return(-1);
116 }
117 resp = tpinterf_pass_output(8); /* 8 s timeout */
118 if (resp) {
119 fclose(srr.openfile);
120 return(resp);
121 }
122 rec_count++;
123 if (rec_count % 100 == 0) {
124 putchar('.');
125 fflush(stdout);
126 }
127 }
128 /* got S7 */
129 fclose(srr.openfile);
130 if (!rec_count) {
131 fprintf(stderr,
132 "%s line %d: S7 without any preceding S3 data records\n",
133 srr.filename, srr.lineno);
134 return(-1);
135 }
136 printf("\nProgramming complete\n");
137 return(0);
138 }
139
140 flashcmd_program_srec(argc, argv, bank)
141 char **argv;
142 {
143 if (argc != 3) {
144 fprintf(stderr, "usage: %s %s image.srec\n", argv[0], argv[1]);
145 return(-1);
146 }
147 return flashcmd_progsrec_gen(bank, argv[2], 0);
148 }
149
150 flashcmd_program_m0(argc, argv, bank)
151 char **argv;
152 {
153 if (argc != 3) {
154 fprintf(stderr, "usage: %s %s image.m0\n", argv[0], argv[1]);
155 return(-1);
156 }
157 return flashcmd_progsrec_gen(bank, argv[2], 1);
158 }