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