From patchwork Wed Apr 25 13:18:02 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Igor Opaniuk X-Patchwork-Id: 134289 Delivered-To: patch@linaro.org Received: by 10.46.151.6 with SMTP id r6csp859861lji; Wed, 25 Apr 2018 06:21:17 -0700 (PDT) X-Google-Smtp-Source: AIpwx49V79XNZxa013w/+0zQ6n4iJOUE4bJcsDCuLDHy3C/YUIdbygIVITzo1qKnL8isqDsW3JRM X-Received: by 10.80.172.164 with SMTP id x33mr38537814edc.270.1524662477637; Wed, 25 Apr 2018 06:21:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524662477; cv=none; d=google.com; s=arc-20160816; b=OdNJW1RrSF/m+t1d9F6CKs4/D1OcvHUp4Ym/MNKu5Iuz0QVd/xhuDoa4GQa7odCrI/ 5QpSVht6gugrYe67Z88v6xD6QcdV8vTrPZzwUv9OTjkHsrooqFCY97ftCelDd3wUPw1X XRvM8ypNSOvy/B9xbGZblxKzzXBIgAzfalBSvqt+gZRgF/GXtjd7CGyaGRYypEDT9s34 /YW+UhsiJUwNhrDQRLx+gpRxVWcF3ZkErmNtBfzJR/0+KOb5a4/VTbqSdHfx8iCwZZm0 vas7RAwGIVU2NWFCyfTsXaQvyDZzuaCGFQi/6ERRU1bV2eHZAexipcO1OO0lAnMHI1xZ YUTQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:cc:references:in-reply-to:message-id :date:to:from:dkim-signature:arc-authentication-results; bh=ZX5zRa/2g5mxDYmZJpOF6B68ee5stjelFXZPBF4syt0=; b=TFaV8LMtjUkpV/Nx9YxjK6+211w2ImIzFhf3QwEL6N8F8e87qmH6+8S6xil1tqEF9D TbNe2iL9of5VYs/OZOpjc33Nk1+vDIE/5MgSQ3jKJnDMtb6a2FZtOvwXM+AfhuRu54bn isgRgx1KtIw0ZY9JP9ydjLgLYBSzgSmbxJYmxWzyUjdfiyUwlNmiddoQ/bs592kggGc+ DG7l+2JoOx54aNQzYQJJAQDshxT209tsQbSxQ9CoRFGv4QNtnZLx6GtRQNs3YSXQcuue S1pPAXbe2P+CVEU+3IDFxnVdhT4JndnRlLvVc6BMNJ3vcUCbXkF0JUzegnik0qjbODCg kVug== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=Pj/tfUee; spf=pass (google.com: best guess record for domain of u-boot-bounces@lists.denx.de designates 81.169.180.215 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.denx.de (dione.denx.de. [81.169.180.215]) by mx.google.com with ESMTP id d16si1005546edn.14.2018.04.25.06.21.17; Wed, 25 Apr 2018 06:21:17 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of u-boot-bounces@lists.denx.de designates 81.169.180.215 as permitted sender) client-ip=81.169.180.215; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=Pj/tfUee; spf=pass (google.com: best guess record for domain of u-boot-bounces@lists.denx.de designates 81.169.180.215 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: by lists.denx.de (Postfix, from userid 105) id 30556C21FCE; Wed, 25 Apr 2018 13:21:01 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 2588CC21F21; Wed, 25 Apr 2018 13:20:59 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 35700C22002; Wed, 25 Apr 2018 13:18:19 +0000 (UTC) Received: from mail-lf0-f65.google.com (mail-lf0-f65.google.com [209.85.215.65]) by lists.denx.de (Postfix) with ESMTPS id ECE2EC21F35 for ; Wed, 25 Apr 2018 13:18:18 +0000 (UTC) Received: by mail-lf0-f65.google.com with SMTP id z130-v6so25433082lff.5 for ; Wed, 25 Apr 2018 06:18:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=CHpxtKNE5yt1Bj4N5dSqMWcG3XGBWhnNegofj5jlI7I=; b=Pj/tfUeeijZu6pvMhov0/P0Ao1THUi+VcJKWyhEjsp1/8lisVoYvQt5KcnmQgwDeg0 4BsBfq6oaEiRgcuiGOHDmLRAkPKEP4MmDccrKEDmus1EpyxX/pNBGBnPzfumO/m0bo8Q fCAT5zMR7ep5NJ6SqGvMafmGJhD1+UiMLuGvU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=CHpxtKNE5yt1Bj4N5dSqMWcG3XGBWhnNegofj5jlI7I=; b=F29q7MOaxVgDe5sMZs5f108C1807ypdp1mEkqy+O3WEH/gQj7t0CENWW0v539zTt/S hjgfYJ+wCtKusIgRKsnPqi7RV5Za5lCi62l+yMcwdXl12VNUNabccKpSadkL1ECJE78/ kpQI+v+nOt+XCqGME6uDjepb+y3LUjLzps4vjdKFw4hTMEIuSQgz4AXfl1mRThIOQ1RP w0To6C2rdCvdjXgLjA8amJ6KXH9FkwhDMHnox2HCVVoA5Ypd6RmJUCCIeEos/70rplrT ySTDu6oO2Ae3ndCg9p8km3EaeJg1XEUC1//P8mD2g5pOTuqIQE6KyhVX52zYLfv+1kCZ cyMQ== X-Gm-Message-State: ALQs6tDFlDgh27hXhv57Ed52tm9xrXuhO6Z9n+lGmFh428A4Ff8ALFO8 99YMgrYYYjdBGC89eDd0QOdwlOEwlvZZUQ== X-Received: by 2002:a19:c1c9:: with SMTP id r192-v6mr13631510lff.108.1524662297961; Wed, 25 Apr 2018 06:18:17 -0700 (PDT) Received: from localhost ([195.238.93.36]) by smtp.gmail.com with ESMTPSA id y132-v6sm412376lfc.56.2018.04.25.06.18.16 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 25 Apr 2018 06:18:17 -0700 (PDT) From: Igor Opaniuk To: u-boot@lists.denx.de Date: Wed, 25 Apr 2018 16:18:02 +0300 Message-Id: <1524662285-19617-6-git-send-email-igor.opaniuk@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1524662285-19617-1-git-send-email-igor.opaniuk@linaro.org> References: <1524662285-19617-1-git-send-email-igor.opaniuk@linaro.org> X-Mailman-Approved-At: Wed, 25 Apr 2018 13:20:57 +0000 Cc: trini@konsulko.com, praneeth@ti.com, misael.lopez@ti.com, joakim.bech@linaro.org Subject: [U-Boot] [PATCH 5/8] avb2.0: add boot states and dm-verity support X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" 1. Add initial support of boot states mode (red, green, yellow) 2. Add functions for enforcing dm-verity configurations Signed-off-by: Igor Opaniuk --- cmd/avb.c | 17 ++++++- common/avb_verify.c | 140 +++++++++++++++++++++++++++++++++++++++++++++++++-- include/avb_verify.h | 19 ++++++- 3 files changed, 171 insertions(+), 5 deletions(-) diff --git a/cmd/avb.c b/cmd/avb.c index d040906..2c15b47 100644 --- a/cmd/avb.c +++ b/cmd/avb.c @@ -218,6 +218,8 @@ int do_avb_verify_part(cmd_tbl_t *cmdtp, int flag, { AvbSlotVerifyResult slot_result; AvbSlotVerifyData *out_data; + char *cmdline; + char *extra_args; bool unlocked = false; int res = CMD_RET_FAILURE; @@ -243,10 +245,23 @@ int do_avb_verify_part(cmd_tbl_t *cmdtp, int flag, "", unlocked, &out_data); switch (slot_result) { case AVB_SLOT_VERIFY_RESULT_OK: + /* Until we don't have support of changing unlock states, we + * assume that we are by default in locked state. + * So in this case we can boot only when verification is + * successful; we also supply in cmdline GREEN boot state + */ printf("Verification passed successfully\n"); /* export additional bootargs to AVB_BOOTARGS env var */ - env_set(AVB_BOOTARGS, out_data->cmdline); + + extra_args = avb_set_state(avb_ops, AVB_GREEN); + if (extra_args) + cmdline = append_cmd_line(out_data->cmdline, + extra_args); + else + cmdline = out_data->cmdline; + + env_set(AVB_BOOTARGS, cmdline); res = CMD_RET_SUCCESS; break; diff --git a/common/avb_verify.c b/common/avb_verify.c index b3d1229..df5e407 100644 --- a/common/avb_verify.c +++ b/common/avb_verify.c @@ -119,6 +119,140 @@ const unsigned char avb_root_pub[1032] = { /** * ============================================================================ + * Boot states support (GREEN, YELLOW, ORANGE, RED) and dm_verity + * ============================================================================ + */ +char *avb_set_state(AvbOps *ops, enum avb_boot_state boot_state) +{ + struct AvbOpsData *data; + char *cmdline = NULL; + + if (!ops) + return NULL; + + data = (struct AvbOpsData *)ops->user_data; + if (!data) + return NULL; + + data->boot_state = boot_state; + switch (boot_state) { + case AVB_GREEN: + cmdline = "androidboot.verifiedbootstate=green"; + break; + case AVB_YELLOW: + cmdline = "androidboot.verifiedbootstate=yellow"; + break; + case AVB_ORANGE: + cmdline = "androidboot.verifiedbootstate=orange"; + case AVB_RED: + break; + } + + return cmdline; +} + +char *append_cmd_line(char *cmdline_orig, char *cmdline_new) +{ + char *cmd_line; + + if (!cmdline_new) + return cmdline_orig; + + if (cmdline_orig) + cmd_line = cmdline_orig; + else + cmd_line = " "; + + cmd_line = avb_strdupv(cmd_line, " ", cmdline_new, NULL); + + return cmd_line; +} + +static int avb_find_dm_args(char **args, char *str) +{ + int i = 0; + + if (!str) + return -1; + + do { + if ((!args[i]) || (i >= AVB_MAX_ARGS)) + return -1; + + if (strstr(args[i], str)) + return i; + + i++; + } while (1); +} + +static char *avb_set_enforce_option(const char *cmdline, const char *option) +{ + char *cmdarg[AVB_MAX_ARGS]; + char *newargs = NULL; + int i = 0; + int total_args; + + memset(cmdarg, 0, sizeof(cmdarg)); + cmdarg[i++] = strtok((char *)cmdline, " "); + + do { + cmdarg[i] = strtok(NULL, " "); + if (!cmdarg[i]) + break; + + if (++i >= AVB_MAX_ARGS) { + printf("%s: Can't handle more then %d args\n", + __func__, i); + return NULL; + } + } while (true); + + total_args = i; + i = avb_find_dm_args(&cmdarg[0], VERITY_TABLE_OPT_LOGGING); + if (i >= 0) { + cmdarg[i] = (char *)option; + } else { + i = avb_find_dm_args(&cmdarg[0], VERITY_TABLE_OPT_RESTART); + if (i < 0) { + printf("%s: No verity options found\n", __func__); + return NULL; + } + + cmdarg[i] = (char *)option; + } + + for (i = 0; i <= total_args; i++) + newargs = append_cmd_line(newargs, cmdarg[i]); + + return newargs; +} + +char *avb_set_ignore_corruption(const char *cmdline) +{ + char *newargs = NULL; + + newargs = avb_set_enforce_option(cmdline, VERITY_TABLE_OPT_LOGGING); + if (newargs) + newargs = append_cmd_line(newargs, + "androidboot.veritymode=eio"); + + return newargs; +} + +char *avb_set_enforce_verity(const char *cmdline) +{ + char *newargs; + + newargs = avb_set_enforce_option(cmdline, VERITY_TABLE_OPT_RESTART); + if (newargs) + newargs = append_cmd_line(newargs, + "androidboot.veritymode=enforcing"); + return newargs; +} + +/** + * ============================================================================ * IO(mmc) auxiliary functions * ============================================================================ */ @@ -478,7 +612,7 @@ static AvbIOResult read_rollback_index(AvbOps *ops, u64 *out_rollback_index) { /* For now we always return 0 as the stored rollback index. */ - printf("TODO: implement %s.\n", __func__); + printf("%s not supported yet\n", __func__); if (out_rollback_index) *out_rollback_index = 0; @@ -502,7 +636,7 @@ static AvbIOResult write_rollback_index(AvbOps *ops, u64 rollback_index) { /* For now this is a no-op. */ - printf("TODO: implement %s.\n", __func__); + printf("%s not supported yet\n", __func__); return AVB_IO_RESULT_OK; } @@ -522,7 +656,7 @@ static AvbIOResult read_is_device_unlocked(AvbOps *ops, bool *out_is_unlocked) { /* For now we always return that the device is unlocked. */ - printf("TODO: implement %s.\n", __func__); + printf("%s not supported yet\n", __func__); *out_is_unlocked = true; diff --git a/include/avb_verify.h b/include/avb_verify.h index fb7ab23..9363ca5 100644 --- a/include/avb_verify.h +++ b/include/avb_verify.h @@ -11,12 +11,23 @@ #include #include -#define ALLOWED_BUF_ALIGN 8 +#define AVB_MAX_ARGS 1024 +#define VERITY_TABLE_OPT_RESTART "restart_on_corruption" +#define VERITY_TABLE_OPT_LOGGING "ignore_corruption" +#define ALLOWED_BUF_ALIGN 8 + +enum avb_boot_state { + AVB_GREEN, + AVB_YELLOW, + AVB_ORANGE, + AVB_RED, +}; struct AvbOpsData { struct AvbOps ops; struct AvbABOps ab_ops; int mmc_dev; + enum avb_boot_state boot_state; }; struct mmc_part { @@ -34,6 +45,12 @@ enum mmc_io_type { AvbOps *avb_ops_alloc(int boot_device); void avb_ops_free(AvbOps *ops); +char *avb_set_state(AvbOps *ops, enum avb_boot_state boot_state); +char *avb_set_enforce_verity(const char *cmdline); +char *avb_set_ignore_corruption(const char *cmdline); + +char *append_cmd_line(char *cmdline_orig, char *cmdline_new); + /** * ============================================================================ * I/O helper inline functions