blob: bc78609e699e6159dc0c2c435d1c7c263aaf55fd [file] [log] [blame]
Andrew Jeffery4fe996c2018-02-27 12:16:48 +10301// SPDX-License-Identifier: Apache-2.0
2// Copyright (C) 2018 IBM Corp.
Andrew Jeffery71eaa732018-08-08 16:44:24 +09303extern "C" {
4#include "flash.h"
5}
6
Deepak Kodihallib6a446f2017-04-29 13:01:49 -05007#include "mboxd_pnor_partition_table.h"
Andrew Jeffery2ceba892018-02-28 17:44:54 +10308#include "pnor_partition_table.hpp"
Deepak Kodihalli017e45c2017-07-12 01:06:30 -05009#include "common.h"
Deepak Kodihallib6a446f2017-04-29 13:01:49 -050010#include "mbox.h"
11#include "pnor_partition_table.hpp"
Deepak Kodihalli64ec3e42017-07-17 06:15:16 -050012#include "config.h"
Deepak Kodihalli6e6aa3a2017-08-28 06:13:43 -050013#include "xyz/openbmc_project/Common/error.hpp"
14#include <phosphor-logging/elog-errors.hpp>
Ratan Gupta3214b512017-05-11 08:58:19 +053015#include <experimental/filesystem>
Andrew Jeffery943aba02018-03-26 15:37:33 +103016#include "vpnor/mboxd_msg.hpp"
Deepak Kodihallib6a446f2017-04-29 13:01:49 -050017
Andrew Jefferyf96bd162018-02-26 13:05:00 +103018int init_vpnor(struct mbox_context *context)
Deepak Kodihallib6a446f2017-04-29 13:01:49 -050019{
Ratan Gupta3214b512017-05-11 08:58:19 +053020 if (context && !context->vpnor)
Deepak Kodihallib6a446f2017-04-29 13:01:49 -050021 {
Andrew Jefferyf96bd162018-02-26 13:05:00 +103022 int rc;
23
Andrew Jefferye4cf6ac2018-03-02 09:05:01 +103024 strncpy(context->paths.ro_loc, PARTITION_FILES_RO_LOC, PATH_MAX);
25 context->paths.ro_loc[PATH_MAX - 1] = '\0';
26 strncpy(context->paths.rw_loc, PARTITION_FILES_RW_LOC, PATH_MAX);
27 context->paths.rw_loc[PATH_MAX - 1] = '\0';
28 strncpy(context->paths.prsv_loc, PARTITION_FILES_PRSV_LOC, PATH_MAX);
29 context->paths.prsv_loc[PATH_MAX - 1] = '\0';
30 strncpy(context->paths.patch_loc, PARTITION_FILES_PATCH_LOC, PATH_MAX);
31 context->paths.prsv_loc[PATH_MAX - 1] = '\0';
Deepak Kodihalli64ec3e42017-07-17 06:15:16 -050032
Andrew Jeffery742a1f62018-03-02 09:26:03 +103033 rc = init_vpnor_from_paths(context);
Andrew Jefferyf96bd162018-02-26 13:05:00 +103034 if (rc < 0)
Andrew Jeffery742a1f62018-03-02 09:26:03 +103035 {
Andrew Jefferyf96bd162018-02-26 13:05:00 +103036 return rc;
Andrew Jeffery742a1f62018-03-02 09:26:03 +103037 }
Ratan Gupta3214b512017-05-11 08:58:19 +053038 }
Andrew Jefferyf96bd162018-02-26 13:05:00 +103039
40 return 0;
Ratan Gupta3214b512017-05-11 08:58:19 +053041}
42
Andrew Jeffery742a1f62018-03-02 09:26:03 +103043int init_vpnor_from_paths(struct mbox_context *context)
Ratan Gupta3214b512017-05-11 08:58:19 +053044{
Andrew Jefferyf96bd162018-02-26 13:05:00 +103045 namespace err = sdbusplus::xyz::openbmc_project::Common::Error;
46 namespace fs = std::experimental::filesystem;
47 namespace vpnor = openpower::virtual_pnor;
Ratan Gupta3214b512017-05-11 08:58:19 +053048
49 if (context && !context->vpnor)
50 {
Andrew Jeffery943aba02018-03-26 15:37:33 +103051 context->handlers = vpnor_mbox_handlers;
52
Andrew Jefferyf96bd162018-02-26 13:05:00 +103053 try
54 {
55 context->vpnor = new vpnor_partition_table;
56 context->vpnor->table =
Andrew Jeffery742a1f62018-03-02 09:26:03 +103057 new openpower::virtual_pnor::partition::Table(context);
Andrew Jefferyf96bd162018-02-26 13:05:00 +103058 }
59 catch (vpnor::TocEntryError &e)
60 {
61 MSG_ERR("%s\n", e.what());
62 phosphor::logging::commit<err::InternalFailure>();
63 return -MBOX_R_SYSTEM_ERROR;
64 }
Deepak Kodihallib6a446f2017-04-29 13:01:49 -050065 }
Andrew Jefferyf96bd162018-02-26 13:05:00 +103066
67 return 0;
Deepak Kodihallib6a446f2017-04-29 13:01:49 -050068}
69
Andrew Jefferyf96bd162018-02-26 13:05:00 +103070int vpnor_copy_bootloader_partition(const struct mbox_context *context)
Deepak Kodihalli017e45c2017-07-12 01:06:30 -050071{
72 // The hostboot bootloader has certain size/offset assumptions, so
73 // we need a special partition table here.
74 // It assumes the PNOR is 64M, the TOC size is 32K, the erase block is
75 // 4K, the page size is 4K.
76 // It also assumes the TOC is at the 'end of pnor - toc size - 1 page size'
77 // offset, and first looks for the TOC here, before proceeding to move up
78 // page by page looking for the TOC. So it is optimal to place the TOC at
79 // this offset.
80 constexpr size_t eraseSize = 0x1000;
81 constexpr size_t pageSize = 0x1000;
82 constexpr size_t pnorSize = 0x4000000;
83 constexpr size_t tocMaxSize = 0x8000;
84 constexpr size_t tocStart = pnorSize - tocMaxSize - pageSize;
85 constexpr auto blPartitionName = "HBB";
Andrew Jefferyf96bd162018-02-26 13:05:00 +103086
87 namespace err = sdbusplus::xyz::openbmc_project::Common::Error;
Andrew Jefferyd976c9c2018-02-27 14:56:07 +103088 namespace fs = std::experimental::filesystem;
Andrew Jefferyf96bd162018-02-26 13:05:00 +103089 namespace vpnor = openpower::virtual_pnor;
Deepak Kodihalli017e45c2017-07-12 01:06:30 -050090
Deepak Kodihalli6e6aa3a2017-08-28 06:13:43 -050091 try
92 {
Andrew Jefferyf96bd162018-02-26 13:05:00 +103093 vpnor_partition_table vtbl{};
Andrew Jeffery742a1f62018-03-02 09:26:03 +103094 struct mbox_context local = *context;
Andrew Jefferyf96bd162018-02-26 13:05:00 +103095 local.vpnor = &vtbl;
96 local.block_size_shift = log_2(eraseSize);
Andrew Jeffery742a1f62018-03-02 09:26:03 +103097
98 openpower::virtual_pnor::partition::Table blTable(&local);
99
100 vtbl.table = &blTable;
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030101
102 size_t tocOffset = 0;
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030103
Deepak Kodihalli6e6aa3a2017-08-28 06:13:43 -0500104 // Copy TOC
Andrew Jefferye0cdd3e2018-08-08 16:51:44 +0930105 flash_copy(&local, tocOffset,
Andrew Jeffery7f9c3432018-03-01 12:07:13 +1030106 static_cast<uint8_t *>(context->mem) + tocStart,
107 blTable.capacity());
Andrew Jefferyf34db312018-03-09 15:27:03 +1030108 const pnor_partition &partition = blTable.partition(blPartitionName);
Deepak Kodihalli6e6aa3a2017-08-28 06:13:43 -0500109 size_t hbbOffset = partition.data.base * eraseSize;
110 uint32_t hbbSize = partition.data.actual;
111 // Copy HBB
Andrew Jefferye0cdd3e2018-08-08 16:51:44 +0930112 flash_copy(&local, hbbOffset,
Andrew Jefferyf34db312018-03-09 15:27:03 +1030113 static_cast<uint8_t *>(context->mem) + hbbOffset, hbbSize);
Deepak Kodihalli6e6aa3a2017-08-28 06:13:43 -0500114 }
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030115 catch (err::InternalFailure &e)
Deepak Kodihalli6e6aa3a2017-08-28 06:13:43 -0500116 {
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030117 phosphor::logging::commit<err::InternalFailure>();
118 return -MBOX_R_SYSTEM_ERROR;
Deepak Kodihalli6e6aa3a2017-08-28 06:13:43 -0500119 }
Andrew Jeffery8fe809e2018-05-17 09:54:32 +0930120 catch (vpnor::ReasonedError &e)
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030121 {
122 MSG_ERR("%s\n", e.what());
123 phosphor::logging::commit<err::InternalFailure>();
124 return -MBOX_R_SYSTEM_ERROR;
125 }
126
127 return 0;
Deepak Kodihalli017e45c2017-07-12 01:06:30 -0500128}
129
Deepak Kodihalli64ec3e42017-07-17 06:15:16 -0500130void destroy_vpnor(struct mbox_context *context)
Deepak Kodihallib6a446f2017-04-29 13:01:49 -0500131{
Andrew Jefferyf34db312018-03-09 15:27:03 +1030132 if (context && context->vpnor)
Deepak Kodihallib6a446f2017-04-29 13:01:49 -0500133 {
134 delete context->vpnor->table;
135 delete context->vpnor;
136 context->vpnor = nullptr;
137 }
138}