Andrew Jeffery | 4fe996c | 2018-02-27 12:16:48 +1030 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (C) 2018 IBM Corp. |
Andrew Jeffery | 71eaa73 | 2018-08-08 16:44:24 +0930 | [diff] [blame] | 3 | extern "C" { |
| 4 | #include "flash.h" |
| 5 | } |
| 6 | |
William A. Kennington III | d5f1d40 | 2018-10-11 13:55:04 -0700 | [diff] [blame] | 7 | #include "config.h" |
| 8 | |
Andrew Jeffery | 2ceba89 | 2018-02-28 17:44:54 +1030 | [diff] [blame] | 9 | #include "pnor_partition_table.hpp" |
William A. Kennington III | d5f1d40 | 2018-10-11 13:55:04 -0700 | [diff] [blame] | 10 | #include "xyz/openbmc_project/Common/error.hpp" |
| 11 | |
| 12 | #include <experimental/filesystem> |
| 13 | #include <phosphor-logging/elog-errors.hpp> |
| 14 | |
Deepak Kodihalli | 017e45c | 2017-07-12 01:06:30 -0500 | [diff] [blame] | 15 | #include "common.h" |
Andrew Jeffery | 26558db | 2018-08-10 00:22:38 +0930 | [diff] [blame] | 16 | #include "mboxd.h" |
William A. Kennington III | d5f1d40 | 2018-10-11 13:55:04 -0700 | [diff] [blame] | 17 | #include "mboxd_pnor_partition_table.h" |
Deepak Kodihalli | b6a446f | 2017-04-29 13:01:49 -0500 | [diff] [blame] | 18 | |
William A. Kennington III | d5f1d40 | 2018-10-11 13:55:04 -0700 | [diff] [blame] | 19 | int init_vpnor(struct mbox_context* context) |
Deepak Kodihalli | b6a446f | 2017-04-29 13:01:49 -0500 | [diff] [blame] | 20 | { |
Ratan Gupta | 3214b51 | 2017-05-11 08:58:19 +0530 | [diff] [blame] | 21 | if (context && !context->vpnor) |
Deepak Kodihalli | b6a446f | 2017-04-29 13:01:49 -0500 | [diff] [blame] | 22 | { |
Andrew Jeffery | f96bd16 | 2018-02-26 13:05:00 +1030 | [diff] [blame] | 23 | int rc; |
| 24 | |
Andrew Jeffery | e4cf6ac | 2018-03-02 09:05:01 +1030 | [diff] [blame] | 25 | strncpy(context->paths.ro_loc, PARTITION_FILES_RO_LOC, PATH_MAX); |
| 26 | context->paths.ro_loc[PATH_MAX - 1] = '\0'; |
| 27 | strncpy(context->paths.rw_loc, PARTITION_FILES_RW_LOC, PATH_MAX); |
| 28 | context->paths.rw_loc[PATH_MAX - 1] = '\0'; |
| 29 | strncpy(context->paths.prsv_loc, PARTITION_FILES_PRSV_LOC, PATH_MAX); |
| 30 | context->paths.prsv_loc[PATH_MAX - 1] = '\0'; |
| 31 | strncpy(context->paths.patch_loc, PARTITION_FILES_PATCH_LOC, PATH_MAX); |
| 32 | context->paths.prsv_loc[PATH_MAX - 1] = '\0'; |
Deepak Kodihalli | 64ec3e4 | 2017-07-17 06:15:16 -0500 | [diff] [blame] | 33 | |
Andrew Jeffery | 742a1f6 | 2018-03-02 09:26:03 +1030 | [diff] [blame] | 34 | rc = init_vpnor_from_paths(context); |
Andrew Jeffery | f96bd16 | 2018-02-26 13:05:00 +1030 | [diff] [blame] | 35 | if (rc < 0) |
Andrew Jeffery | 742a1f6 | 2018-03-02 09:26:03 +1030 | [diff] [blame] | 36 | { |
Andrew Jeffery | f96bd16 | 2018-02-26 13:05:00 +1030 | [diff] [blame] | 37 | return rc; |
Andrew Jeffery | 742a1f6 | 2018-03-02 09:26:03 +1030 | [diff] [blame] | 38 | } |
Ratan Gupta | 3214b51 | 2017-05-11 08:58:19 +0530 | [diff] [blame] | 39 | } |
Andrew Jeffery | f96bd16 | 2018-02-26 13:05:00 +1030 | [diff] [blame] | 40 | |
| 41 | return 0; |
Ratan Gupta | 3214b51 | 2017-05-11 08:58:19 +0530 | [diff] [blame] | 42 | } |
| 43 | |
William A. Kennington III | d5f1d40 | 2018-10-11 13:55:04 -0700 | [diff] [blame] | 44 | int init_vpnor_from_paths(struct mbox_context* context) |
Ratan Gupta | 3214b51 | 2017-05-11 08:58:19 +0530 | [diff] [blame] | 45 | { |
Andrew Jeffery | f96bd16 | 2018-02-26 13:05:00 +1030 | [diff] [blame] | 46 | namespace err = sdbusplus::xyz::openbmc_project::Common::Error; |
| 47 | namespace fs = std::experimental::filesystem; |
| 48 | namespace vpnor = openpower::virtual_pnor; |
Ratan Gupta | 3214b51 | 2017-05-11 08:58:19 +0530 | [diff] [blame] | 49 | |
| 50 | if (context && !context->vpnor) |
| 51 | { |
Andrew Jeffery | f96bd16 | 2018-02-26 13:05:00 +1030 | [diff] [blame] | 52 | try |
| 53 | { |
| 54 | context->vpnor = new vpnor_partition_table; |
| 55 | context->vpnor->table = |
Andrew Jeffery | 742a1f6 | 2018-03-02 09:26:03 +1030 | [diff] [blame] | 56 | new openpower::virtual_pnor::partition::Table(context); |
Andrew Jeffery | f96bd16 | 2018-02-26 13:05:00 +1030 | [diff] [blame] | 57 | } |
William A. Kennington III | d5f1d40 | 2018-10-11 13:55:04 -0700 | [diff] [blame] | 58 | catch (vpnor::TocEntryError& e) |
Andrew Jeffery | f96bd16 | 2018-02-26 13:05:00 +1030 | [diff] [blame] | 59 | { |
| 60 | MSG_ERR("%s\n", e.what()); |
| 61 | phosphor::logging::commit<err::InternalFailure>(); |
Andrew Jeffery | 8eab215 | 2018-08-09 23:47:29 +0930 | [diff] [blame] | 62 | return -EINVAL; |
Andrew Jeffery | f96bd16 | 2018-02-26 13:05:00 +1030 | [diff] [blame] | 63 | } |
Deepak Kodihalli | b6a446f | 2017-04-29 13:01:49 -0500 | [diff] [blame] | 64 | } |
Andrew Jeffery | f96bd16 | 2018-02-26 13:05:00 +1030 | [diff] [blame] | 65 | |
| 66 | return 0; |
Deepak Kodihalli | b6a446f | 2017-04-29 13:01:49 -0500 | [diff] [blame] | 67 | } |
| 68 | |
William A. Kennington III | d5f1d40 | 2018-10-11 13:55:04 -0700 | [diff] [blame] | 69 | int vpnor_copy_bootloader_partition(const struct mbox_context* context) |
Deepak Kodihalli | 017e45c | 2017-07-12 01:06:30 -0500 | [diff] [blame] | 70 | { |
| 71 | // The hostboot bootloader has certain size/offset assumptions, so |
| 72 | // we need a special partition table here. |
| 73 | // It assumes the PNOR is 64M, the TOC size is 32K, the erase block is |
| 74 | // 4K, the page size is 4K. |
| 75 | // It also assumes the TOC is at the 'end of pnor - toc size - 1 page size' |
| 76 | // offset, and first looks for the TOC here, before proceeding to move up |
| 77 | // page by page looking for the TOC. So it is optimal to place the TOC at |
| 78 | // this offset. |
| 79 | constexpr size_t eraseSize = 0x1000; |
| 80 | constexpr size_t pageSize = 0x1000; |
| 81 | constexpr size_t pnorSize = 0x4000000; |
| 82 | constexpr size_t tocMaxSize = 0x8000; |
| 83 | constexpr size_t tocStart = pnorSize - tocMaxSize - pageSize; |
| 84 | constexpr auto blPartitionName = "HBB"; |
Andrew Jeffery | f96bd16 | 2018-02-26 13:05:00 +1030 | [diff] [blame] | 85 | |
| 86 | namespace err = sdbusplus::xyz::openbmc_project::Common::Error; |
Andrew Jeffery | d976c9c | 2018-02-27 14:56:07 +1030 | [diff] [blame] | 87 | namespace fs = std::experimental::filesystem; |
Andrew Jeffery | f96bd16 | 2018-02-26 13:05:00 +1030 | [diff] [blame] | 88 | namespace vpnor = openpower::virtual_pnor; |
Deepak Kodihalli | 017e45c | 2017-07-12 01:06:30 -0500 | [diff] [blame] | 89 | |
Deepak Kodihalli | 6e6aa3a | 2017-08-28 06:13:43 -0500 | [diff] [blame] | 90 | try |
| 91 | { |
Andrew Jeffery | f96bd16 | 2018-02-26 13:05:00 +1030 | [diff] [blame] | 92 | vpnor_partition_table vtbl{}; |
Andrew Jeffery | 742a1f6 | 2018-03-02 09:26:03 +1030 | [diff] [blame] | 93 | struct mbox_context local = *context; |
Andrew Jeffery | f96bd16 | 2018-02-26 13:05:00 +1030 | [diff] [blame] | 94 | local.vpnor = &vtbl; |
| 95 | local.block_size_shift = log_2(eraseSize); |
Andrew Jeffery | 742a1f6 | 2018-03-02 09:26:03 +1030 | [diff] [blame] | 96 | |
| 97 | openpower::virtual_pnor::partition::Table blTable(&local); |
| 98 | |
| 99 | vtbl.table = &blTable; |
Andrew Jeffery | f96bd16 | 2018-02-26 13:05:00 +1030 | [diff] [blame] | 100 | |
| 101 | size_t tocOffset = 0; |
Andrew Jeffery | f96bd16 | 2018-02-26 13:05:00 +1030 | [diff] [blame] | 102 | |
Deepak Kodihalli | 6e6aa3a | 2017-08-28 06:13:43 -0500 | [diff] [blame] | 103 | // Copy TOC |
Andrew Jeffery | e0cdd3e | 2018-08-08 16:51:44 +0930 | [diff] [blame] | 104 | flash_copy(&local, tocOffset, |
William A. Kennington III | d5f1d40 | 2018-10-11 13:55:04 -0700 | [diff] [blame] | 105 | static_cast<uint8_t*>(context->mem) + tocStart, |
Andrew Jeffery | 7f9c343 | 2018-03-01 12:07:13 +1030 | [diff] [blame] | 106 | blTable.capacity()); |
William A. Kennington III | d5f1d40 | 2018-10-11 13:55:04 -0700 | [diff] [blame] | 107 | const pnor_partition& partition = blTable.partition(blPartitionName); |
Deepak Kodihalli | 6e6aa3a | 2017-08-28 06:13:43 -0500 | [diff] [blame] | 108 | size_t hbbOffset = partition.data.base * eraseSize; |
| 109 | uint32_t hbbSize = partition.data.actual; |
| 110 | // Copy HBB |
Andrew Jeffery | e0cdd3e | 2018-08-08 16:51:44 +0930 | [diff] [blame] | 111 | flash_copy(&local, hbbOffset, |
William A. Kennington III | d5f1d40 | 2018-10-11 13:55:04 -0700 | [diff] [blame] | 112 | static_cast<uint8_t*>(context->mem) + hbbOffset, hbbSize); |
Deepak Kodihalli | 6e6aa3a | 2017-08-28 06:13:43 -0500 | [diff] [blame] | 113 | } |
William A. Kennington III | d5f1d40 | 2018-10-11 13:55:04 -0700 | [diff] [blame] | 114 | catch (err::InternalFailure& e) |
Deepak Kodihalli | 6e6aa3a | 2017-08-28 06:13:43 -0500 | [diff] [blame] | 115 | { |
Andrew Jeffery | f96bd16 | 2018-02-26 13:05:00 +1030 | [diff] [blame] | 116 | phosphor::logging::commit<err::InternalFailure>(); |
Andrew Jeffery | 8eab215 | 2018-08-09 23:47:29 +0930 | [diff] [blame] | 117 | return -EIO; |
Deepak Kodihalli | 6e6aa3a | 2017-08-28 06:13:43 -0500 | [diff] [blame] | 118 | } |
William A. Kennington III | d5f1d40 | 2018-10-11 13:55:04 -0700 | [diff] [blame] | 119 | catch (vpnor::ReasonedError& e) |
Andrew Jeffery | f96bd16 | 2018-02-26 13:05:00 +1030 | [diff] [blame] | 120 | { |
| 121 | MSG_ERR("%s\n", e.what()); |
| 122 | phosphor::logging::commit<err::InternalFailure>(); |
Andrew Jeffery | 8eab215 | 2018-08-09 23:47:29 +0930 | [diff] [blame] | 123 | return -EIO; |
Andrew Jeffery | f96bd16 | 2018-02-26 13:05:00 +1030 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | return 0; |
Deepak Kodihalli | 017e45c | 2017-07-12 01:06:30 -0500 | [diff] [blame] | 127 | } |
| 128 | |
William A. Kennington III | d5f1d40 | 2018-10-11 13:55:04 -0700 | [diff] [blame] | 129 | void destroy_vpnor(struct mbox_context* context) |
Deepak Kodihalli | b6a446f | 2017-04-29 13:01:49 -0500 | [diff] [blame] | 130 | { |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 131 | if (context && context->vpnor) |
Deepak Kodihalli | b6a446f | 2017-04-29 13:01:49 -0500 | [diff] [blame] | 132 | { |
| 133 | delete context->vpnor->table; |
| 134 | delete context->vpnor; |
| 135 | context->vpnor = nullptr; |
| 136 | } |
| 137 | } |