blob: db53dc89c997bb82df93168078ce66079a94211a [file] [log] [blame]
Andrew Jeffery4fe996c2018-02-27 12:16:48 +10301// SPDX-License-Identifier: Apache-2.0
2// Copyright (C) 2018 IBM Corp.
Deepak Kodihallib6a446f2017-04-29 13:01:49 -05003#include "mboxd_pnor_partition_table.h"
Andrew Jeffery2ceba892018-02-28 17:44:54 +10304#include "pnor_partition_table.hpp"
Deepak Kodihalli017e45c2017-07-12 01:06:30 -05005#include "common.h"
Deepak Kodihallib6a446f2017-04-29 13:01:49 -05006#include "mbox.h"
Deepak Kodihalli017e45c2017-07-12 01:06:30 -05007#include "mboxd_flash.h"
Deepak Kodihallib6a446f2017-04-29 13:01:49 -05008#include "pnor_partition_table.hpp"
Deepak Kodihalli64ec3e42017-07-17 06:15:16 -05009#include "config.h"
Deepak Kodihalli6e6aa3a2017-08-28 06:13:43 -050010#include "xyz/openbmc_project/Common/error.hpp"
11#include <phosphor-logging/elog-errors.hpp>
Ratan Gupta3214b512017-05-11 08:58:19 +053012#include <experimental/filesystem>
Deepak Kodihallib6a446f2017-04-29 13:01:49 -050013
Andrew Jefferyf96bd162018-02-26 13:05:00 +103014int init_vpnor(struct mbox_context *context)
Deepak Kodihallib6a446f2017-04-29 13:01:49 -050015{
Ratan Gupta3214b512017-05-11 08:58:19 +053016 if (context && !context->vpnor)
Deepak Kodihallib6a446f2017-04-29 13:01:49 -050017 {
Andrew Jefferyf96bd162018-02-26 13:05:00 +103018 int rc;
19
Andrew Jefferye4cf6ac2018-03-02 09:05:01 +103020 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 Kodihalli64ec3e42017-07-17 06:15:16 -050028
Andrew Jeffery742a1f62018-03-02 09:26:03 +103029 rc = init_vpnor_from_paths(context);
Andrew Jefferyf96bd162018-02-26 13:05:00 +103030 if (rc < 0)
Andrew Jeffery742a1f62018-03-02 09:26:03 +103031 {
Andrew Jefferyf96bd162018-02-26 13:05:00 +103032 return rc;
Andrew Jeffery742a1f62018-03-02 09:26:03 +103033 }
Ratan Gupta3214b512017-05-11 08:58:19 +053034 }
Andrew Jefferyf96bd162018-02-26 13:05:00 +103035
36 return 0;
Ratan Gupta3214b512017-05-11 08:58:19 +053037}
38
Andrew Jeffery742a1f62018-03-02 09:26:03 +103039int init_vpnor_from_paths(struct mbox_context *context)
Ratan Gupta3214b512017-05-11 08:58:19 +053040{
Andrew Jefferyf96bd162018-02-26 13:05:00 +103041 namespace err = sdbusplus::xyz::openbmc_project::Common::Error;
42 namespace fs = std::experimental::filesystem;
43 namespace vpnor = openpower::virtual_pnor;
Ratan Gupta3214b512017-05-11 08:58:19 +053044
45 if (context && !context->vpnor)
46 {
Andrew Jefferyf96bd162018-02-26 13:05:00 +103047 try
48 {
49 context->vpnor = new vpnor_partition_table;
50 context->vpnor->table =
Andrew Jeffery742a1f62018-03-02 09:26:03 +103051 new openpower::virtual_pnor::partition::Table(context);
Andrew Jefferyf96bd162018-02-26 13:05:00 +103052 }
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 Kodihallib6a446f2017-04-29 13:01:49 -050059 }
Andrew Jefferyf96bd162018-02-26 13:05:00 +103060
61 return 0;
Deepak Kodihallib6a446f2017-04-29 13:01:49 -050062}
63
Andrew Jefferyf96bd162018-02-26 13:05:00 +103064int vpnor_copy_bootloader_partition(const struct mbox_context *context)
Deepak Kodihalli017e45c2017-07-12 01:06:30 -050065{
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 Jefferyf96bd162018-02-26 13:05:00 +103080
81 namespace err = sdbusplus::xyz::openbmc_project::Common::Error;
Andrew Jefferyd976c9c2018-02-27 14:56:07 +103082 namespace fs = std::experimental::filesystem;
Andrew Jefferyf96bd162018-02-26 13:05:00 +103083 namespace vpnor = openpower::virtual_pnor;
Deepak Kodihalli017e45c2017-07-12 01:06:30 -050084
Deepak Kodihalli6e6aa3a2017-08-28 06:13:43 -050085 try
86 {
Andrew Jefferyf96bd162018-02-26 13:05:00 +103087 vpnor_partition_table vtbl{};
Andrew Jeffery742a1f62018-03-02 09:26:03 +103088 struct mbox_context local = *context;
Andrew Jefferyf96bd162018-02-26 13:05:00 +103089 local.vpnor = &vtbl;
90 local.block_size_shift = log_2(eraseSize);
Andrew Jeffery742a1f62018-03-02 09:26:03 +103091
92 openpower::virtual_pnor::partition::Table blTable(&local);
93
94 vtbl.table = &blTable;
Andrew Jefferyf96bd162018-02-26 13:05:00 +103095
96 size_t tocOffset = 0;
Andrew Jefferyf96bd162018-02-26 13:05:00 +103097
Deepak Kodihalli6e6aa3a2017-08-28 06:13:43 -050098 // Copy TOC
99 copy_flash(&local, tocOffset,
Andrew Jeffery7f9c3432018-03-01 12:07:13 +1030100 static_cast<uint8_t *>(context->mem) + tocStart,
101 blTable.capacity());
Andrew Jefferyf34db312018-03-09 15:27:03 +1030102 const pnor_partition &partition = blTable.partition(blPartitionName);
Deepak Kodihalli6e6aa3a2017-08-28 06:13:43 -0500103 size_t hbbOffset = partition.data.base * eraseSize;
104 uint32_t hbbSize = partition.data.actual;
105 // Copy HBB
106 copy_flash(&local, hbbOffset,
Andrew Jefferyf34db312018-03-09 15:27:03 +1030107 static_cast<uint8_t *>(context->mem) + hbbOffset, hbbSize);
Deepak Kodihalli6e6aa3a2017-08-28 06:13:43 -0500108 }
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030109 catch (err::InternalFailure &e)
Deepak Kodihalli6e6aa3a2017-08-28 06:13:43 -0500110 {
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030111 phosphor::logging::commit<err::InternalFailure>();
112 return -MBOX_R_SYSTEM_ERROR;
Deepak Kodihalli6e6aa3a2017-08-28 06:13:43 -0500113 }
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030114 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 Kodihalli017e45c2017-07-12 01:06:30 -0500122}
123
Deepak Kodihalli64ec3e42017-07-17 06:15:16 -0500124void destroy_vpnor(struct mbox_context *context)
Deepak Kodihallib6a446f2017-04-29 13:01:49 -0500125{
Andrew Jefferyf34db312018-03-09 15:27:03 +1030126 if (context && context->vpnor)
Deepak Kodihallib6a446f2017-04-29 13:01:49 -0500127 {
128 delete context->vpnor->table;
129 delete context->vpnor;
130 context->vpnor = nullptr;
131 }
132}