Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

在 Android Fastboot 中擴充自訂命令

編輯:關於 Android 程式設計

Fastboot 是 Android 預設的一種除錯方式,其優點在於可以於進入 Linux kernel 之前進行操作。本文說明如何在 Fastboot 中註冊並實作自訂命令。

Fastboot 預設支援的命令如下:

usage: fastboot [ <option> ] <command>
commands:
    update <filename>              reflash device from update.zip
    flashall                       flash boot + recovery + system
    flash <partition> [ <filename> ] write a file to a flash partition
    erase <partition>              erase a flash partition
    format <partition>             format a flash partition
    getvar <variable>              display a bootloader variable
    boot <kernel> [ <ramdisk> ]      download and boot kernel
    flash:raw boot <kernel> [ <ramdisk> ] create bootimage and flash it
    devices                        list all connected devices
    continue                       continue with autoboot
    reboot                         reboot device normally
    reboot-bootloader              reboot device into bootloader
    help                           show this help message
options:
    -w                 erase userdata and cache
    -u                 do not first erase partition before formatting
    -s <specific device> specify device serial number or path to device port
    -l                 with "devices", lists device paths
    -p <product>         specify product name
    -c <cmdline>         override kernel commandline
    -i <vendor id>       specify a custom USB vendor id
    -b <base_addr>       specify a custom kernel base address
    -n <page size>       specify the nand page size. default: 2048
    -S <size>[K|M|G]    automatically sparse files greater than size. 0 to disable

Fastboot 另外提供了擴充命令的機制,格式如下:

fastboot oem command args

以下以 fastboot oem hello test 為例,說明擴充的三個步驟。

步驟一:註冊新命令

在 bootable/bootloader/lk/app/mt_boot/fastboot.c 的 fastboot_init 函式中加入註冊:

// 第一個參數是命令的名稱
// 第二個參數是命令的執行函式
// 第三個參數表示在 security IC 中是否仍提供此命令
fastboot_register("oem hello", cmd_oem_hello, FALSE);

步驟二:實作執行函式

void cmd_oem_hello(const char *arg, void *data, unsigned size) {
    // 注意 args 是以 command 結束處開始,即 " args"
    if (!strncmp(arg, " OK", strlen(" OK"))) {
        fastboot_okey("OK");
    } else {
        fastboot_fail("Not OK");
    }
}

步驟三:與 PC 端互動

可使用以下三個已定義好的函式與 PC 端溝通:

fastboot_okey(const char* result);
fastboot_fail(const char* reason);
fastboot_info(const char* reason);

需特別注意,這三個函式所輸出的字串長度皆不可超過 64-1-4 = 59 個字元。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved