blob: e5e1ad822a7bc9649359edb751851b9c041dbad1 [file] [log] [blame]
Andrew Jefferyc7d19472018-08-08 11:43:08 +09301// SPDX-License-Identifier: Apache-2.0
2// Copyright (C) 2018 IBM Corp.
3#include "config.h"
4
5extern "C" {
Andrew Jeffery26558db2018-08-10 00:22:38 +09306#include "mboxd.h"
Andrew Jefferyc7d19472018-08-08 11:43:08 +09307#include "protocol.h"
8#include "vpnor/protocol.h"
9}
10
11#include "vpnor/pnor_partition_table.hpp"
12
13/* XXX: Maybe this should be a method on a class? */
14static bool vpnor_partition_is_readonly(const pnor_partition &part)
15{
16 return part.data.user.data[1] & PARTITION_READONLY;
17}
18
19typedef int (*create_window_fn)(struct mbox_context *context,
20 struct protocol_create_window *io);
21
22static int generic_vpnor_create_window(struct mbox_context *context,
23 struct protocol_create_window *io,
24 create_window_fn create_window)
25{
26 if (io->req.ro)
27 {
28 return create_window(context, io);
29 }
30
31 /* Only allow write windows on regions mapped by the ToC as writeable */
32 size_t offset = io->req.offset;
33 offset <<= context->block_size_shift;
34 try
35 {
36 const pnor_partition &part = context->vpnor->table->partition(offset);
37 if (vpnor_partition_is_readonly(part))
38 {
39 return -EPERM;
40 }
41 }
42 catch (const openpower::virtual_pnor::UnmappedOffset &e)
43 {
44 /*
45 * Writes to unmapped areas are not meaningful, so deny the request.
46 * This removes the ability for a compromised host to abuse unused
47 * space if any data was to be persisted (which it isn't).
48 */
49 return -EACCES;
50 }
51
52 return create_window(context, io);
53}
54
55int protocol_v1_vpnor_create_window(struct mbox_context *context,
56 struct protocol_create_window *io)
57{
58 return generic_vpnor_create_window(context, io, protocol_v1_create_window);
59}
60
61int protocol_v2_vpnor_create_window(struct mbox_context *context,
62 struct protocol_create_window *io)
63{
64 return generic_vpnor_create_window(context, io, protocol_v2_create_window);
65}