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