blob: 9aed5fdad08c4057da017d1a80b6403bba38bacf [file] [log] [blame]
Deepak Kodihalli6c2fa902017-05-01 06:36:02 -05001/*
2 * Mailbox Daemon Window Helpers
3 *
4 * Copyright 2017 IBM
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19
20#include <stdint.h>
21#include <stdlib.h>
22#include <syslog.h>
23
24extern "C" {
25#include "common.h"
26}
27
28#include "mboxd_flash.h"
29#include "mboxd_pnor_partition_table.h"
30
31/*
32 * copy_flash() - Copy data from the virtual pnor into a provided buffer
33 * @context: The mbox context pointer
34 * @offset: The pnor offset to copy from (bytes)
35 * @mem: The buffer to copy into (must be of atleast 'size' bytes)
36 * @size: The number of bytes to copy
37 *
38 * Return: 0 on success otherwise negative error code
39 */
40int copy_flash(struct mbox_context* context, uint32_t offset, void* mem,
41 uint32_t size)
42{
43 int rc = 0;
44
45 MSG_DBG("Copy virtual pnor to %p for size 0x%.8x from offset 0x%.8x\n",
46 mem, size, offset);
47
48 /* The virtual PNOR partition table starts at offset 0 in the virtual
49 * pnor image. Check if host asked for an offset that lies within the
50 * partition table.
51 */
52 size_t sz =
53 vpnor_get_partition_table_size(context) << context->block_size_shift;
54 if (offset < sz)
55 {
56 const struct pnor_partition_table* table =
57 vpnor_get_partition_table(context);
58 memcpy(mem,
59 ((uint8_t*)table) + offset,
60 min_u32(sz - offset, size));
61 }
62
63 return rc;
64}