4 * Copyright (C) 2013-11-07 K.Ohta <whatisthis.sowhat ai gmail.com>
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,
9 * or (at your option) any later version.
10 * This library / program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this library; see the file COPYING. If not, write to the
17 * Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
20 * As a special exception, if you link this(includeed from sdcc) library
21 * with other files, some of which are compiled with SDCC,
22 * to produce an executable, this library does not by itself cause
23 * the resulting executable to be covered by the GNU General Public License.
24 * This exception does not however invalidate any other reasons why
25 * the executable file might be covered by the GNU General Public License.
30 #include <pic18fregs.h> /* ONLY FOR PIC18x */
37 #include "shell_strutl.h"
39 unsigned char c2h(unsigned char c)
41 if(c < '1') return 0xff;
42 if(c > 'F') return 0xff;
43 if(c <= '9') return c - '0';
44 if(c >= 'A') return c - 'A' + 10;
49 unsigned char hex2byte(unsigned char *p)
54 if((h >= 0x10) || (l >= 0x10)) return 0;
58 unsigned char check_eol(unsigned char *p)
62 for(i = 0; i < 2; i++){
72 if(c < '0') return 0xff;
73 if(c > 'F') return 0xff;
78 unsigned char migrate_hex(unsigned char *p)
82 for(i = 0; i < 2; i++){
86 if((c > '9') && (c < 'A')) return 0;
91 char str_shexheader(unsigned char *s, unsigned char *file)
98 if(check_eol(s) != 0) return TERM_NULL;
100 if((s[0] != 'S') || (s[1] != '0')) return TERM_NONSREC;
102 if(migrate_hex(&s[2]) == 0) return TERM_NONSREC;
103 bytes = hex2byte(&s[2]);
104 if(bytes <= 2) return TERM_SRECERR;
105 for(i = 4; i < 8; i++) if(s[i] != '0') return TERM_NONHDR;
110 if(migrate_hex(&s[i]) == 0) return TERM_SRECERR;
119 if(migrate_hex(&s[i]) == 0) return TERM_SRECERR;
122 if(c != sum) return TERM_SUMERR;
129 * Accept only S2 and S8.
131 char str_shex2bin(unsigned char *s, unsigned char *p, unsigned long *addr, unsigned char *len)
133 unsigned char sum = 0;
137 unsigned char h, l, l2;
140 if(s[0] != 'S') return TERM_NONSREC;
141 if(s[1] == '2') { // Data core
142 for(i = 2; i < 8; i += 2) if(migrate_hex(&s[i]) == 0) return TERM_SRECERR;
143 bytes = hex2byte(&s[2]);
144 if(bytes <= 4) return TERM_SRECERR;
148 l2 = hex2byte(&s[8]);
149 *addr = (h * 65536) | (l << 8) | l2;
150 sum = sum + h + l + l2;
156 if(migrate_hex(&s[i]) == 0) return TERM_SRECERR;
164 if(migrate_hex(&s[i]) == 0) return TERM_SUMERR;
167 if(c != sum) return TERM_SUMERR;
171 } else if(s[1] == '8') {
172 if((s[2] != '0') || (s[3] != '3')) return TERM_SRECERR;
173 for(i = 4; i < 10; i += 2) if(migrate_hex(&s[i]) == 0) return TERM_SRECERR;
177 l2 = hex2byte(&s[8]);
178 c = hex2byte(&s[10]);
179 *addr = (h * 65536) | (l << 8) | l2;
181 sum = ~(sum + h + l + l2);
182 if(c != sum) return TERM_SUMERR;
191 void bin2hex(char *s, unsigned char v)
201 unsigned char str_bin2hex(unsigned char *s, unsigned char *p, unsigned long addr, unsigned char len)
210 if(len > 254) len = 254;
215 c = (addr >> 16) & 255;
219 c = (addr >> 8) & 255;
228 for(pv = 0; pv < len ; pv++){
235 bin2hex(&s[pp], sum);
242 unsigned char str_put_shexheader(unsigned char *s, char *filename)
250 len = shell_strlen(filename);
251 if(len > 252) len = 252;
253 bin2hex(&s[2], len + 3);
255 for(i = 4; i < 8; i++) s[i] = '0';
259 bin2hex(&s[i], filename[pp]);
271 unsigned char str_put_shexfooter(unsigned char *s)
281 for(pp = 4; pp < 10; pp++) s[pp] = '0';
283 bin2hex(&s[pp], sum);
291 unsigned char shell_strlen(char *s)
295 if(s[i] == '\0') break;
301 unsigned char shell_gettok(char *dst, char *src)
305 while(src[i] != '\0') {
306 if(src[i] == ' ') break;
307 if(src[i] == '\t') break;
316 int shell_strcmp(char *from, char *to)
322 f = shell_strlen(from);
323 t = shell_strlen(to);
327 if(from[p] != to[p]) return -1;
334 long term_getuint(char *pool)
342 if((c < '0') || (c > '9')) return -1;
352 void term_printnum(char *pool, unsigned int num)
355 unsigned char i, j, ii;
357 for(i = 0; i < 5; i++){
372 for(j = 0; j <= ii; j++) {
373 pool[j] = p[i] + '0';