@@ -247,6 +247,10 @@ static void usage(void)
247
247
fprintf (stderr , "\tset ssh telnet uart to default enable\n" );
248
248
fprintf (stderr , "mitool model\n" );
249
249
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" );
250
254
}
251
255
252
256
static const unsigned int crc32tab [] = {
@@ -466,6 +470,22 @@ static int lock_mtd(int t)
466
470
467
471
}
468
472
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
+
469
489
char * get_model (char * pid )
470
490
{
471
491
char * model = "unknown" ;
@@ -541,37 +561,130 @@ static int calc_img_crc()
541
561
return 0 ;
542
562
}
543
563
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
+ }
544
596
545
- static int open_ssh ()
597
+ static int set_ssh ()
546
598
{
547
- int i ,j , ret = 0 ;
599
+ int i , ret = 0 ;
548
600
549
601
if (load_buf ()< 0 )
550
602
return -1 ;
603
+ if (check_unlock ()){
604
+ printf ("mtd is not unlocked\n" );
605
+ return -1 ;
606
+ }
551
607
i = GetSubStrPos (buf , "model" );
552
608
printf ("model=%s\n" , get_model (& buf [i + 6 ]));
553
609
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
+ }
556
617
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
+ }
559
625
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
+ }
562
633
ret = calc_img_crc ();
563
634
system ("sed -i 's/channel=.*/channel=\"debug\"/g' /etc/init.d/dropbear" );
564
635
system ("/etc/init.d/dropbear start &" );
565
636
return ret ;
566
637
}
567
638
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
+ }
568
681
int main (int argc , char * * argv )
569
682
{
570
683
int ret ;
571
- if (argc != 2 )
684
+ if (argc < 2 )
572
685
usage ();
573
686
else if (!strcmp (argv [1 ], "hack" )) {
574
- ret = open_ssh ();
687
+ ret = set_ssh ();
575
688
if (ret < 0 ) {
576
689
exit (1 );
577
690
}
@@ -588,6 +701,12 @@ int main(int argc, char **argv)
588
701
printf ("ssh/telnet default usesrname:root password:%s\n" ,password );
589
702
} else if (!strcmp (argv [1 ], "model" )){
590
703
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 );
591
710
} else
592
711
usage ();
593
712
0 commit comments