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