Skip to content

Commit 0486008

Browse files
committed
fix the bug of wrong offset, add modify sn
1 parent 04e5bf1 commit 0486008

File tree

2 files changed

+137
-10
lines changed

2 files changed

+137
-10
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,11 @@ Show password only
2020
Show model only
2121

2222
`/tmp/mitool.sh model`
23+
24+
Show sn only
25+
26+
`/tmp/mitool.sh sn`
27+
28+
set sn(automatic reboot and relock the partition lock)
29+
30+
`/tmp/mitool.sh setsn xxxxxxxxxxxxx`

mitool.c

Lines changed: 129 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,10 @@ static void usage(void)
247247
fprintf(stderr, "\tset ssh telnet uart to default enable\n");
248248
fprintf(stderr, "mitool model\n");
249249
fprintf(stderr, "\tshow model\n");
250+
fprintf(stderr, "mitool sn\n");
251+
fprintf(stderr, "\tshow sn\n");
252+
fprintf(stderr, "mitool setsn xxxxxxxx\n");
253+
fprintf(stderr, "\tset sn\n");
250254
}
251255

252256
static const unsigned int crc32tab[] = {
@@ -466,6 +470,22 @@ static int lock_mtd(int t)
466470

467471
}
468472

473+
static int check_unlock()
474+
{
475+
FILE *fd;
476+
unsigned char temp[4];
477+
fd = fopen("/dev/mtd10", "rb");
478+
if (fd < 0)
479+
return -1;
480+
memset(temp, 0, sizeof(temp));
481+
fseek(fd, 0, SEEK_SET);
482+
fread(temp, 4, 1,fd);
483+
fclose(fd);
484+
if(temp[0] != 0xA5)
485+
return 1;
486+
return 0;
487+
}
488+
469489
char *get_model(char *pid)
470490
{
471491
char *model = "unknown";
@@ -541,37 +561,130 @@ static int calc_img_crc()
541561
return 0;
542562
}
543563

564+
static int add_bdata(char *s)
565+
{
566+
int i;
567+
if(s == NULL)
568+
return -1;
569+
for(i = 0; i < BUFSIZE; i++ ){
570+
if(buf[i] == 0x0 && buf[i+1] == 0x0){
571+
sprintf(&buf[i+1], "%s", s);
572+
break;
573+
}
574+
}
575+
return 0;
576+
}
577+
578+
static int verify_sn(char *s)
579+
{
580+
char sn[16];
581+
int i;
582+
memset(sn, 0, sizeof(sn));
583+
snprintf(sn, sizeof(sn), "%s", s);
584+
if(sn[5] != '/')
585+
return -1;
586+
for(i = 0; i < 5; i++){
587+
if(sn[i] < '0' || sn[i] > '9')
588+
return -1;
589+
}
590+
for(i = 6; i < 15; i++){
591+
if((sn[i] < '0' || sn[i] > '9') && (sn[i] < 'A' || sn[i] > 'Z'))
592+
return -1;
593+
}
594+
return 0;
595+
}
544596

545-
static int open_ssh()
597+
static int set_ssh()
546598
{
547-
int i,j, ret = 0;
599+
int i, ret = 0;
548600

549601
if(load_buf()<0)
550602
return -1;
603+
if(check_unlock()){
604+
printf("mtd is not unlocked\n");
605+
return -1;
606+
}
551607
i = GetSubStrPos(buf, "model");
552608
printf("model=%s\n", get_model(&buf[i+6]));
553609
i = GetSubStrPos(buf, "ssh_en");
554-
printf("get ssh_en=%c", buf[i+7]);
555-
buf[i+7] = '1';//ssh
610+
if(i < 0){
611+
printf("not found ssh_en, add it.\n");
612+
add_bdata("ssh_en=1");
613+
}else{
614+
printf("get ssh_en=%c", buf[i+7]);
615+
buf[i+7] = '1';//ssh
616+
}
556617
i = GetSubStrPos(buf, "telnet_en");
557-
printf(" telnet_en=%c", buf[i+10]);
558-
buf[i+10] = '1';//telnet
618+
if(i < 0){
619+
printf("not found telnet_en, add it.\n");
620+
add_bdata("telnet_en=1");
621+
}else{
622+
printf(" telnet_en=%c", buf[i+10]);
623+
buf[i+10] = '1';//telnet
624+
}
559625
i = GetSubStrPos(buf, "uart_en");
560-
printf(" uart_en=%c\n", buf[i+8]);
561-
buf[i+8] = '1';//uart
626+
if(i < 0){
627+
printf("not found uart_en, add it.\n");
628+
add_bdata("uart_en=1");
629+
}else{
630+
printf(" uart_en=%c\n", buf[i+8]);
631+
buf[i+8] = '1';//uart
632+
}
562633
ret = calc_img_crc();
563634
system("sed -i 's/channel=.*/channel=\"debug\"/g' /etc/init.d/dropbear");
564635
system("/etc/init.d/dropbear start &");
565636
return ret;
566637
}
567638

639+
static int show_sn()
640+
{
641+
int i;
642+
if(load_buf()<0)
643+
return -1;
644+
i = GetSubStrPos(buf, "model");
645+
printf("model=%s\n", get_model(&buf[i+6]));
646+
i = GetSubStrPos(buf, "SN");
647+
printf("get %s\n", &buf[i]);
648+
return 0;
649+
}
650+
651+
static int set_sn(char *sn)
652+
{
653+
int i, ret = 0;
654+
655+
if(load_buf()<0)
656+
return -1;
657+
if(check_unlock()){
658+
printf("mtd is not unlocked\n");
659+
return -1;
660+
}
661+
if(strlen(sn) != 15 || verify_sn(sn) < 0){
662+
printf("SN is wrong,%s\n",sn);
663+
return -1;
664+
}
665+
i = GetSubStrPos(buf, "model");
666+
printf("model=%s\n", get_model(&buf[i+6]));
667+
i = GetSubStrPos(buf, "SN");
668+
if(i < 0){
669+
char tmp[19];
670+
printf("not found SN, add it.\n");
671+
memset(tmp, 0, sizeof(tmp));
672+
snprintf(tmp, sizeof(tmp), "SN=%s", sn);
673+
add_bdata(tmp);
674+
}else{
675+
printf("get %s\n", &buf[i]);
676+
sprintf(&buf[i], "SN=%s", sn);
677+
}
678+
ret = calc_img_crc();
679+
return ret;
680+
}
568681
int main(int argc, char **argv)
569682
{
570683
int ret;
571-
if (argc != 2)
684+
if (argc < 2)
572685
usage();
573686
else if (!strcmp(argv[1], "hack")) {
574-
ret = open_ssh();
687+
ret = set_ssh();
575688
if (ret < 0) {
576689
exit(1);
577690
}
@@ -588,6 +701,12 @@ int main(int argc, char **argv)
588701
printf("ssh/telnet default usesrname:root password:%s\n",password);
589702
} else if (!strcmp(argv[1], "model")){
590703
model_show();
704+
} else if (!strcmp(argv[1], "sn")){
705+
show_sn();
706+
} else if (!strcmp(argv[1], "setsn") && argv[2]){
707+
set_sn(argv[2]);
708+
printf("automatic lock mtd and reboot\n");
709+
lock_mtd(1);
591710
} else
592711
usage();
593712

0 commit comments

Comments
 (0)