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