From patchwork Mon May 21 23:04:21 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sam Protsenko X-Patchwork-Id: 136503 Delivered-To: patch@linaro.org Received: by 2002:a2e:9706:0:0:0:0:0 with SMTP id r6-v6csp883585lji; Mon, 21 May 2018 16:04:32 -0700 (PDT) X-Google-Smtp-Source: AB8JxZqkfIK1/OXzTGsoAI6z58/q9LFQmutw+a+O24car4U6i5EIv7oaNPtWPXVNuwJqdBDiCZTm X-Received: by 2002:a50:88c3:: with SMTP id d61-v6mr26243550edd.122.1526943872070; Mon, 21 May 2018 16:04:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526943872; cv=none; d=google.com; s=arc-20160816; b=JQaMcdRJs/rav7NVXngp7XnyDK511w+drnFtOKe+e0RLoFLRxcKaQifO9kCoQ7e+rw ZAftdz5uqeNNGGszgaJ9TdjXolWiQ+QsDHQfmh9Kz8hByUlKT1EU70PC0aD+PjpieuBu DpHYAO4AzP8cQfOWmsuYAjjeMV/kYirw/EncCsZjsQymt6sTRlHOH2avxzdqfUdp9VLb 2QWkAaQRONJkgMZIHGQ2eS/MLIKK99q2IuJuEvr1MYg1K4U8o4/9HUOpW+E1roAlguXy GdTL2y1l0mCkjxE9q2NVBuROhnyDri3BjMhyuinGCR78s6r+oSwrlV2p8SvbYEf7AUUi Bddw== 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:message-id:date:to:from :dkim-signature:arc-authentication-results; bh=BPsXPTQA/7+zPljnEyGGbxX2bd6SzLBh0RU6TJxwiwg=; b=xzot3fH0vxaSvhHwREQH2REQcEwLrJA5ryIHHfvg7i+bgjcQnJBgYH6BVBkptejpq5 YiHDtGC/luc9mPZrqi7cgk5eVcaRPcONEYe7aoxYubTs6lOMurcL58+Vqdxq5iYgnj6Y ZAlhja25OkHrHerF677cFr6LXyK7Xmkds+xrwgfcXM4WY8Wi5bpU0Za7SJEjzUKT4+cQ vFcZe9tf1QYzh+Y6jTacWsTKsY+QCFARMawU61kuvujMv788lQCSgFjdMJNuiCanwDsL +Xfv7lVXuUutHphGKWF2UCHT8TEtXQvOb4cxwRdrnztR69ROGMRWYkYf+dohHOESRd1h sDbw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=PT9gpIPX; 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 y5-v6si3611476edp.247.2018.05.21.16.04.31; Mon, 21 May 2018 16:04:32 -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=PT9gpIPX; 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 1F51FC21D8A; Mon, 21 May 2018 23:04:27 +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 DD378C21C51; Mon, 21 May 2018 23:04:25 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 49995C21C2F; Mon, 21 May 2018 23:04:25 +0000 (UTC) Received: from mail-lf0-f67.google.com (mail-lf0-f67.google.com [209.85.215.67]) by lists.denx.de (Postfix) with ESMTPS id BD82EC21C27 for ; Mon, 21 May 2018 23:04:24 +0000 (UTC) Received: by mail-lf0-f67.google.com with SMTP id w202-v6so26228139lff.12 for ; Mon, 21 May 2018 16:04:24 -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; bh=3mq5CRK2iDVs4blBaqQLoATHlBIXVHQSRddZm2t+H0g=; b=PT9gpIPXl0dBmHIdz31J6FbXLLMl93ASsRAKol5NURoBKfzQcSWFQKe+N/l8RN5IR6 CFvWZtd/JIpJCNNRk3KWz6H1WFC7XpLECNOHFUhipwdKkpQz8y8nY23xi1AaAB3s1iEu os9QRQbedtzHyCzoWeQcPnb25AqBdYq3NKzuM= 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; bh=3mq5CRK2iDVs4blBaqQLoATHlBIXVHQSRddZm2t+H0g=; b=SR4dk7zVq8TRg5PtPcPIgww8LPO6SwIWqh4IIS8Z4s+FVYLk2kaIHdQfwJLjW39Kr6 +z8cwcBmXxKKPpbKyO5VBf5ujfu1/DcY0OOEM19Sv3RHbgabAL+F7n5PsPQi0TcdNd4R 6LKgPcyikMm1ZWDadDhJ+TGap36gKsw6hFGeQhopd5vCxNFl0FdG0x9fn1k7LlTyfp05 URFgtLOZoAAwdzwLPoEgpybX3od8+x8fr1rWf0gybvvuFwc+dav1WJllvj/5HtyaNyLD QzaEAxfo2WX+NHOVQbUT1W6OLAWSMtqoLBlIfxEMOzCYdjDjX0ZbpLxUvfb5vjEkSlE9 VQ2Q== X-Gm-Message-State: ALKqPwfewWlgkI8wcChbtzP1QneYnj2jOBCjgZ9E6B6bRAqL7LmYQ5/7 GQqwi4ki0/nh/yNSm3uu+iKo7QiMazg= X-Received: by 2002:a19:b2c2:: with SMTP id t63-v6mr3895829lfk.27.1526943863773; Mon, 21 May 2018 16:04:23 -0700 (PDT) Received: from localhost ([195.238.92.132]) by smtp.gmail.com with ESMTPSA id u130-v6sm3658124lff.40.2018.05.21.16.04.22 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 21 May 2018 16:04:22 -0700 (PDT) From: Sam Protsenko To: u-boot@lists.denx.de Date: Tue, 22 May 2018 02:04:21 +0300 Message-Id: <20180521230421.4627-1-semen.protsenko@linaro.org> X-Mailer: git-send-email 2.17.0 Cc: Praneeth Bajjuri , Tom Rini , Alejandro Hernandez Subject: [U-Boot] [PATCH] disk: efi: Correct backing up the MBR boot code 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" In commit e163a931af34 ("cmd: gpt: backup boot code before writing MBR") there was added the procedure for storing old boot code when doing "gpt write". But instead of storing just backup code, the whole MBR was stored, and only specific fields were replaced further, keeping everything else intact. That's obviously not what we want. Fix the code to actually store only old boot code and zero out everything else. This fixes next testing case: => mmc write $loadaddr 0x0 0x7b => gpt write mmc 1 $partitions In case when $loadaddr address and further memory contains 0xff, the board was bricked (ROM-code probably didn't like partition entries that were clobbered with 0xff). With this patch applied, commands above don't brick the board. Signed-off-by: Sam Protsenko Cc: Alejandro Hernandez Tested-by: Andy Shevchenko --- disk/part_efi.c | 6 ++++-- include/part_efi.h | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/disk/part_efi.c b/disk/part_efi.c index 72e3997b84..5c1039f013 100644 --- a/disk/part_efi.c +++ b/disk/part_efi.c @@ -350,8 +350,6 @@ static int set_protective_mbr(struct blk_desc *dev_desc) { /* Setup the Protective MBR */ ALLOC_CACHE_ALIGN_BUFFER_PAD(legacy_mbr, p_mbr, 1, dev_desc->blksz); - memset(p_mbr, 0, sizeof(*p_mbr)); - if (p_mbr == NULL) { printf("%s: calloc failed!\n", __func__); return -1; @@ -363,6 +361,10 @@ static int set_protective_mbr(struct blk_desc *dev_desc) return -1; } + /* Clear all data in MBR except of backed up boot code */ + memset((char *)p_mbr + MSDOS_MBR_BOOT_CODE_SIZE, 0, sizeof(*p_mbr) - + MSDOS_MBR_BOOT_CODE_SIZE); + /* Append signature */ p_mbr->signature = MSDOS_MBR_SIGNATURE; p_mbr->partition_record[0].sys_ind = EFI_PMBR_OSTYPE_EFI_GPT; diff --git a/include/part_efi.h b/include/part_efi.h index 6065c571f3..8525770445 100644 --- a/include/part_efi.h +++ b/include/part_efi.h @@ -20,6 +20,7 @@ #include #define MSDOS_MBR_SIGNATURE 0xAA55 +#define MSDOS_MBR_BOOT_CODE_SIZE 440 #define EFI_PMBR_OSTYPE_EFI 0xEF #define EFI_PMBR_OSTYPE_EFI_GPT 0xEE @@ -111,7 +112,7 @@ typedef struct _gpt_entry { } __packed gpt_entry; typedef struct _legacy_mbr { - u8 boot_code[440]; + u8 boot_code[MSDOS_MBR_BOOT_CODE_SIZE]; __le32 unique_mbr_signature; __le16 unknown; struct partition partition_record[4];