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