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 | |
Deepak Kodihalli | 64ec3e4 | 2017-07-17 06:15:16 -0500 | [diff] [blame] | 16 | void 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 | { |
Deepak Kodihalli | 64ec3e4 | 2017-07-17 06:15:16 -0500 | [diff] [blame] | 20 | strcpy(context->paths.ro_loc, PARTITION_FILES_RO_LOC); |
| 21 | strcpy(context->paths.rw_loc, PARTITION_FILES_RW_LOC); |
| 22 | strcpy(context->paths.prsv_loc, PARTITION_FILES_PRSV_LOC); |
Adriana Kobylak | c71dfd7 | 2017-07-22 11:10:43 -0500 | [diff] [blame] | 23 | strcpy(context->paths.patch_loc, PARTITION_FILES_PATCH_LOC); |
Deepak Kodihalli | 64ec3e4 | 2017-07-17 06:15:16 -0500 | [diff] [blame] | 24 | |
Andrew Jeffery | d976c9c | 2018-02-27 14:56:07 +1030 | [diff] [blame^] | 25 | vpnor_create_partition_table_from_path(context, PARTITION_FILES_RO_LOC); |
Ratan Gupta | 3214b51 | 2017-05-11 08:58:19 +0530 | [diff] [blame] | 26 | } |
| 27 | } |
| 28 | |
| 29 | void vpnor_create_partition_table_from_path(struct mbox_context *context, |
| 30 | const char *path) |
| 31 | { |
| 32 | std::experimental::filesystem::path dir(path); |
| 33 | |
| 34 | if (context && !context->vpnor) |
| 35 | { |
| 36 | context->vpnor = new vpnor_partition_table; |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 37 | context->vpnor->table = new openpower::virtual_pnor::partition::Table( |
| 38 | std::move(dir), 1 << context->erase_size_shift, |
| 39 | context->flash_size); |
Deepak Kodihalli | b6a446f | 2017-04-29 13:01:49 -0500 | [diff] [blame] | 40 | } |
| 41 | } |
| 42 | |
| 43 | size_t vpnor_get_partition_table_size(const struct mbox_context *context) |
| 44 | { |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 45 | return context && context->vpnor ? context->vpnor->table->size() : 0; |
Deepak Kodihalli | b6a446f | 2017-04-29 13:01:49 -0500 | [diff] [blame] | 46 | } |
| 47 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 48 | const struct pnor_partition_table * |
| 49 | vpnor_get_partition_table(const struct mbox_context *context) |
Deepak Kodihalli | b6a446f | 2017-04-29 13:01:49 -0500 | [diff] [blame] | 50 | { |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 51 | return context && context->vpnor ? &(context->vpnor->table->getHostTable()) |
| 52 | : nullptr; |
Deepak Kodihalli | b6a446f | 2017-04-29 13:01:49 -0500 | [diff] [blame] | 53 | } |
| 54 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 55 | const struct pnor_partition * |
| 56 | vpnor_get_partition(const struct mbox_context *context, const size_t offset) |
Deepak Kodihalli | b6a446f | 2017-04-29 13:01:49 -0500 | [diff] [blame] | 57 | { |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 58 | return context && context->vpnor |
| 59 | ? &(context->vpnor->table->partition(offset)) |
| 60 | : nullptr; |
Deepak Kodihalli | b6a446f | 2017-04-29 13:01:49 -0500 | [diff] [blame] | 61 | } |
| 62 | |
Deepak Kodihalli | 017e45c | 2017-07-12 01:06:30 -0500 | [diff] [blame] | 63 | void vpnor_copy_bootloader_partition(const struct mbox_context *context) |
| 64 | { |
| 65 | // The hostboot bootloader has certain size/offset assumptions, so |
| 66 | // we need a special partition table here. |
| 67 | // It assumes the PNOR is 64M, the TOC size is 32K, the erase block is |
| 68 | // 4K, the page size is 4K. |
| 69 | // It also assumes the TOC is at the 'end of pnor - toc size - 1 page size' |
| 70 | // offset, and first looks for the TOC here, before proceeding to move up |
| 71 | // page by page looking for the TOC. So it is optimal to place the TOC at |
| 72 | // this offset. |
| 73 | constexpr size_t eraseSize = 0x1000; |
| 74 | constexpr size_t pageSize = 0x1000; |
| 75 | constexpr size_t pnorSize = 0x4000000; |
| 76 | constexpr size_t tocMaxSize = 0x8000; |
| 77 | constexpr size_t tocStart = pnorSize - tocMaxSize - pageSize; |
| 78 | constexpr auto blPartitionName = "HBB"; |
Andrew Jeffery | d976c9c | 2018-02-27 14:56:07 +1030 | [diff] [blame^] | 79 | namespace fs = std::experimental::filesystem; |
Deepak Kodihalli | 017e45c | 2017-07-12 01:06:30 -0500 | [diff] [blame] | 80 | |
Andrew Jeffery | d976c9c | 2018-02-27 14:56:07 +1030 | [diff] [blame^] | 81 | openpower::virtual_pnor::partition::Table blTable( |
| 82 | fs::path{PARTITION_FILES_RO_LOC}, eraseSize, pnorSize); |
| 83 | |
Deepak Kodihalli | 017e45c | 2017-07-12 01:06:30 -0500 | [diff] [blame] | 84 | vpnor_partition_table vtbl{}; |
| 85 | vtbl.table = &blTable; |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 86 | struct mbox_context local |
| 87 | { |
| 88 | }; |
Deepak Kodihalli | 017e45c | 2017-07-12 01:06:30 -0500 | [diff] [blame] | 89 | local.vpnor = &vtbl; |
| 90 | local.block_size_shift = log_2(eraseSize); |
| 91 | memcpy(&local.paths, &context->paths, sizeof(local.paths)); |
| 92 | |
| 93 | size_t tocOffset = 0; |
Deepak Kodihalli | 7ee307c | 2017-07-12 03:41:08 -0500 | [diff] [blame] | 94 | uint32_t tocSize = blTable.size() * eraseSize; |
Deepak Kodihalli | 6e6aa3a | 2017-08-28 06:13:43 -0500 | [diff] [blame] | 95 | using namespace phosphor::logging; |
| 96 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 97 | try |
| 98 | { |
| 99 | // Copy TOC |
| 100 | copy_flash(&local, tocOffset, |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 101 | static_cast<uint8_t *>(context->mem) + tocStart, tocSize); |
| 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 | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 109 | catch (InternalFailure &e) |
Deepak Kodihalli | 6e6aa3a | 2017-08-28 06:13:43 -0500 | [diff] [blame] | 110 | { |
| 111 | commit<InternalFailure>(); |
| 112 | } |
Deepak Kodihalli | 017e45c | 2017-07-12 01:06:30 -0500 | [diff] [blame] | 113 | } |
| 114 | |
Deepak Kodihalli | 64ec3e4 | 2017-07-17 06:15:16 -0500 | [diff] [blame] | 115 | void destroy_vpnor(struct mbox_context *context) |
Deepak Kodihalli | b6a446f | 2017-04-29 13:01:49 -0500 | [diff] [blame] | 116 | { |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 117 | if (context && context->vpnor) |
Deepak Kodihalli | b6a446f | 2017-04-29 13:01:49 -0500 | [diff] [blame] | 118 | { |
| 119 | delete context->vpnor->table; |
| 120 | delete context->vpnor; |
| 121 | context->vpnor = nullptr; |
| 122 | } |
| 123 | } |