~xnox/+junk/temp

« back to all changes in this revision

Viewing changes to core/fastboot/fastboot.c

  • Committer: Package Import Robot
  • Author(s): Loïc Minier
  • Date: 2013-02-18 16:33:46 UTC
  • mfrom: (3.1.3 raring)
  • Revision ID: package-import@ubuntu.com-20130218163346-5nrw4cgh7t4t0k8a
Tags: 4.2.2+git20130218-1
* Add myself to Uploaders with Marcin's permission; also sponsor NEW
  android-tools-fsutils binary package.
* Improve long descriptions a bit.

Show diffs side-by-side

added added

removed removed

Lines of Context:
70
70
static const char *cmdline = 0;
71
71
static int wipe_data = 0;
72
72
static unsigned short vendor_id = 0;
 
73
static int long_listing = 0;
73
74
static int64_t sparse_limit = -1;
74
75
static int64_t target_sparse_limit = -1;
75
76
 
156
157
    char *data;
157
158
    int sz;
158
159
    int fd;
 
160
    int errno_tmp;
159
161
 
160
162
    data = 0;
161
163
    fd = open(fn, O_RDONLY);
176
178
    return data;
177
179
 
178
180
oops:
 
181
    errno_tmp = errno;
179
182
    close(fd);
180
183
    if(data != 0) free(data);
 
184
    errno = errno_tmp;
181
185
    return 0;
182
186
}
183
187
#endif
184
188
 
185
189
int match_fastboot(usb_ifc_info *info)
186
190
{
 
191
    return match_fastboot_with_serial(info, serial);
 
192
}
 
193
 
 
194
int match_fastboot_with_serial(usb_ifc_info *info, const char *local_serial)
 
195
{
187
196
    if(!(vendor_id && (info->dev_vendor == vendor_id)) &&
188
197
       (info->dev_vendor != 0x18d1) &&  // Google
189
198
       (info->dev_vendor != 0x8087) &&  // Intel
201
210
    if(info->ifc_class != 0xff) return -1;
202
211
    if(info->ifc_subclass != 0x42) return -1;
203
212
    if(info->ifc_protocol != 0x03) return -1;
204
 
    // require matching serial number if a serial number is specified
 
213
    // require matching serial number or device path if requested
205
214
    // at the command line with the -s option.
206
 
    if (serial && strcmp(serial, info->serial_number) != 0) return -1;
 
215
    if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
 
216
                   strcmp(local_serial, info->device_path) != 0)) return -1;
207
217
    return 0;
208
218
}
209
219
 
210
220
int list_devices_callback(usb_ifc_info *info)
211
221
{
212
 
    if (match_fastboot(info) == 0) {
 
222
    if (match_fastboot_with_serial(info, NULL) == 0) {
213
223
        char* serial = info->serial_number;
214
224
        if (!info->writable) {
215
225
            serial = "no permissions"; // like "adb devices"
218
228
            serial = "????????????";
219
229
        }
220
230
        // output compatible with "adb devices"
221
 
        printf("%s\tfastboot\n", serial);
 
231
        if (!long_listing) {
 
232
            printf("%s\tfastboot\n", serial);
 
233
        } else if (!info->device_path) {
 
234
            printf("%-22s fastboot\n", serial);
 
235
        } else {
 
236
            printf("%-22s fastboot %s\n", serial, info->device_path);
 
237
        }
222
238
    }
223
239
 
224
240
    return -1;
271
287
            "  help                                     show this help message\n"
272
288
            "\n"
273
289
            "options:\n"
274
 
            "  -w                                       erase userdata and cache\n"
275
 
            "  -s <serial number>                       specify device serial number\n"
 
290
            "  -w                                       erase userdata and cache (and format\n"
 
291
            "                                           if supported by partition type)\n"
 
292
            "  -u                                       do not first erase partition before\n"
 
293
            "                                           formatting\n"
 
294
            "  -s <specific device>                     specify device serial number\n"
 
295
            "                                           or path to device port\n"
 
296
            "  -l                                       with \"devices\", lists device paths\n"
276
297
            "  -p <product>                             specify product name\n"
277
298
            "  -c <cmdline>                             override kernel commandline\n"
278
299
            "  -i <vendor id>                           specify a custom USB vendor id\n"
298
319
 
299
320
    kdata = load_file(kernel, &ksize);
300
321
    if(kdata == 0) {
301
 
        fprintf(stderr, "cannot load '%s'\n", kernel);
 
322
        fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
302
323
        return 0;
303
324
    }
304
325
 
318
339
    if(ramdisk) {
319
340
        rdata = load_file(ramdisk, &rsize);
320
341
        if(rdata == 0) {
321
 
            fprintf(stderr,"cannot load '%s'\n", ramdisk);
 
342
            fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
322
343
            return  0;
323
344
        }
324
345
    }
544
565
    return 0;
545
566
}
546
567
 
 
568
/* Until we get lazy inode table init working in make_ext4fs, we need to
 
569
 * erase partitions of type ext4 before flashing a filesystem so no stale
 
570
 * inodes are left lying around.  Otherwise, e2fsck gets very upset.
 
571
 */
 
572
static int needs_erase(const char *part)
 
573
{
 
574
    /* The function fb_format_supported() currently returns the value
 
575
     * we want, so just call it.
 
576
     */
 
577
     return fb_format_supported(usb, part);
 
578
}
 
579
 
547
580
void do_flash(usb_handle *usb, const char *pname, const char *fname)
548
581
{
549
582
    int64_t sz64;
564
597
    } else {
565
598
        unsigned int sz;
566
599
        data = load_file(fname, &sz);
567
 
        if (data == 0) die("cannot load '%s'\n", fname);
 
600
        if (data == 0) die("cannot load '%s': %s\n", fname, strerror(errno));
568
601
        fb_queue_flash(pname, data, sz);
569
602
    }
570
603
}
579
612
    fb_queue_command("signature", "installing signature");
580
613
}
581
614
 
582
 
void do_update(char *fn)
 
615
void do_update(char *fn, int erase_first)
583
616
{
584
617
    void *zdata;
585
618
    unsigned zsize;
592
625
    fb_queue_query_save("product", cur_product, sizeof(cur_product));
593
626
 
594
627
    zdata = load_file(fn, &zsize);
595
 
    if (zdata == 0) die("failed to load '%s'", fn);
 
628
    if (zdata == 0) die("failed to load '%s': %s", fn, strerror(errno));
596
629
 
597
630
    zip = init_zipfile(zdata, zsize);
598
631
    if(zip == 0) die("failed to access zipdata in '%s'");
617
650
    data = unzip_file(zip, "boot.img", &sz);
618
651
    if (data == 0) die("update package missing boot.img");
619
652
    do_update_signature(zip, "boot.sig");
 
653
    if (erase_first && needs_erase("boot")) {
 
654
        fb_queue_erase("boot");
 
655
    }
620
656
    fb_queue_flash("boot", data, sz);
621
657
 
622
658
    data = unzip_file(zip, "recovery.img", &sz);
623
659
    if (data != 0) {
624
660
        do_update_signature(zip, "recovery.sig");
 
661
        if (erase_first && needs_erase("recovery")) {
 
662
            fb_queue_erase("recovery");
 
663
        }
625
664
        fb_queue_flash("recovery", data, sz);
626
665
    }
627
666
 
628
667
    data = unzip_file(zip, "system.img", &sz);
629
668
    if (data == 0) die("update package missing system.img");
630
669
    do_update_signature(zip, "system.sig");
 
670
    if (erase_first && needs_erase("system")) {
 
671
        fb_queue_erase("system");
 
672
    }
631
673
    fb_queue_flash("system", data, sz);
632
674
}
633
675
 
649
691
    fb_queue_command("signature", "installing signature");
650
692
}
651
693
 
652
 
void do_flashall(void)
 
694
void do_flashall(int erase_first)
653
695
{
654
696
    char *fname;
655
697
    void *data;
662
704
    fname = find_item("info", product);
663
705
    if (fname == 0) die("cannot find android-info.txt");
664
706
    data = load_file(fname, &sz);
665
 
    if (data == 0) die("could not load android-info.txt");
 
707
    if (data == 0) die("could not load android-info.txt: %s", strerror(errno));
666
708
    setup_requirements(data, sz);
667
709
 
668
710
    fname = find_item("boot", product);
669
711
    data = load_file(fname, &sz);
670
 
    if (data == 0) die("could not load boot.img");
 
712
    if (data == 0) die("could not load boot.img: %s", strerror(errno));
671
713
    do_send_signature(fname);
 
714
    if (erase_first && needs_erase("boot")) {
 
715
        fb_queue_erase("boot");
 
716
    }
672
717
    fb_queue_flash("boot", data, sz);
673
718
 
674
719
    fname = find_item("recovery", product);
675
720
    data = load_file(fname, &sz);
676
721
    if (data != 0) {
677
722
        do_send_signature(fname);
 
723
        if (erase_first && needs_erase("recovery")) {
 
724
            fb_queue_erase("recovery");
 
725
        }
678
726
        fb_queue_flash("recovery", data, sz);
679
727
    }
680
728
 
681
729
    fname = find_item("system", product);
682
730
    data = load_file(fname, &sz);
683
 
    if (data == 0) die("could not load system.img");
 
731
    if (data == 0) die("could not load system.img: %s", strerror(errno));
684
732
    do_send_signature(fname);
 
733
    if (erase_first && needs_erase("system")) {
 
734
        fb_queue_erase("system");
 
735
    }
685
736
    fb_queue_flash("system", data, sz);
686
737
}
687
738
 
752
803
    int wants_wipe = 0;
753
804
    int wants_reboot = 0;
754
805
    int wants_reboot_bootloader = 0;
 
806
    int erase_first = 1;
755
807
    void *data;
756
808
    unsigned sz;
757
809
    unsigned page_size = 2048;
764
816
    serial = getenv("ANDROID_SERIAL");
765
817
 
766
818
    while (1) {
767
 
        c = getopt_long(argc, argv, "wb:n:s:S:p:c:i:m:h", &longopts, NULL);
 
819
        c = getopt_long(argc, argv, "wub:n:s:S:lp:c:i:m:h", &longopts, NULL);
768
820
        if (c < 0) {
769
821
            break;
770
822
        }
773
825
        case 'w':
774
826
            wants_wipe = 1;
775
827
            break;
 
828
        case 'u':
 
829
            erase_first = 0;
 
830
            break;
776
831
        case 'b':
777
832
            base_addr = strtoul(optarg, 0, 16);
778
833
            break;
789
844
                    die("invalid sparse limit");
790
845
            }
791
846
            break;
 
847
        case 'l':
 
848
            long_listing = 1;
 
849
            break;
792
850
        case 'p':
793
851
            product = optarg;
794
852
            break;
829
887
        return 0;
830
888
    }
831
889
 
 
890
    if (argc > 0 && !strcmp(*argv, "help")) {
 
891
        usage();
 
892
        return 0;
 
893
    }
 
894
 
832
895
    usb = open_device();
833
896
 
834
897
    while (argc > 0) {
838
901
            skip(2);
839
902
        } else if(!strcmp(*argv, "erase")) {
840
903
            require(2);
 
904
 
 
905
            if (fb_format_supported(usb, argv[1])) {
 
906
                fprintf(stderr, "******** Did you mean to fastboot format this partition?\n");
 
907
            }
 
908
 
841
909
            fb_queue_erase(argv[1]);
842
910
            skip(2);
843
911
        } else if(!strcmp(*argv, "format")) {
844
912
            require(2);
 
913
            if (erase_first && needs_erase(argv[1])) {
 
914
                fb_queue_erase(argv[1]);
 
915
            }
845
916
            fb_queue_format(argv[1], 0);
846
917
            skip(2);
847
918
        } else if(!strcmp(*argv, "signature")) {
848
919
            require(2);
849
920
            data = load_file(argv[1], &sz);
850
 
            if (data == 0) die("could not load '%s'", argv[1]);
 
921
            if (data == 0) die("could not load '%s': %s", argv[1], strerror(errno));
851
922
            if (sz != 256) die("signature must be 256 bytes");
852
923
            fb_queue_download("signature", data, sz);
853
924
            fb_queue_command("signature", "installing signature");
889
960
                skip(2);
890
961
            }
891
962
            if (fname == 0) die("cannot determine image filename for '%s'", pname);
 
963
            if (erase_first && needs_erase(pname)) {
 
964
                fb_queue_erase(pname);
 
965
            }
892
966
            do_flash(usb, pname, fname);
893
967
        } else if(!strcmp(*argv, "flash:raw")) {
894
968
            char *pname = argv[1];
906
980
            fb_queue_flash(pname, data, sz);
907
981
        } else if(!strcmp(*argv, "flashall")) {
908
982
            skip(1);
909
 
            do_flashall();
 
983
            do_flashall(erase_first);
910
984
            wants_reboot = 1;
911
985
        } else if(!strcmp(*argv, "update")) {
912
986
            if (argc > 1) {
913
 
                do_update(argv[1]);
 
987
                do_update(argv[1], erase_first);
914
988
                skip(2);
915
989
            } else {
916
 
                do_update("update.zip");
 
990
                do_update("update.zip", erase_first);
917
991
                skip(1);
918
992
            }
919
993
            wants_reboot = 1;
920
994
        } else if(!strcmp(*argv, "oem")) {
921
995
            argc = do_oem_command(argc, argv);
922
 
        } else if (!strcmp(*argv, "help")) {
923
 
            usage();
924
 
            return 0;
925
996
        } else {
926
997
            usage();
927
998
            return 1;
940
1011
        fb_queue_command("reboot-bootloader", "rebooting into bootloader");
941
1012
    }
942
1013
 
 
1014
    if (fb_queue_is_empty())
 
1015
        return 0;
 
1016
 
943
1017
    status = fb_execute_queue(usb);
944
1018
    return (status) ? 1 : 0;
945
1019
}