summaryrefslogtreecommitdiffstats
path: root/common/IVTDataSource.cpp
blob: 88c4753e9b44d963cb72a35191bb3c90fdf464d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
 * File:	DataSource.h
 *
 * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
 *
 * Freescale Semiconductor, Inc.
 * Proprietary & Confidential
 *
 * This source code and the algorithms implemented therein constitute
 * confidential information and may comprise trade secrets of Freescale Semiconductor, Inc.
 * or its associates, and any use thereof is subject to the terms and
 * conditions of the Confidential Disclosure Agreement pursual to which this
 * source code was originally received.
 */

#include "IVTDataSource.h"
#include "DataTarget.h"
#include "EndianUtilities.h"
#include <algorithm>
#include <stdlib.h>
#include <string.h>

using namespace elftosb;

IVTDataSource::IVTDataSource()
:   DataSource(),
    DataSource::Segment((DataSource&)*this),
    m_isSelfSet(false)
{
    // Init the IVT structure.
    memset(&m_ivt, 0, sizeof(m_ivt));
    hab_hdr_t hdr = IVT_HDR(sizeof(m_ivt), HAB_VERSION);
    m_ivt.hdr = hdr;
}

unsigned IVTDataSource::getData(unsigned offset, unsigned maxBytes, uint8_t * buffer)
{
    // Bail if the offset is out of range.
    if (offset >= sizeof(m_ivt))
    {
        return 0;
    }
    
    // If we have an associated target, and the IVT self pointer is not set, then
    // fill in the self pointer from the target address.
    if (m_target && !m_isSelfSet)
    {
        m_ivt.self = ENDIAN_HOST_TO_LITTLE_U32(m_target->getBeginAddress());
    }
    
    // Truncate max bytes at the end of the IVT.
    maxBytes = std::min<unsigned>(maxBytes, sizeof(m_ivt) - offset);
    
    // Copy into output buffer.
    if (maxBytes)
    {
        memcpy(buffer, (uint8_t *)&m_ivt + offset, maxBytes);
    }
    
    return maxBytes;
}

unsigned IVTDataSource::getLength()
{
    return sizeof(m_ivt);
}

//! The IVT has a natural location if its self pointer was explicitly specified.
//!
bool IVTDataSource::hasNaturalLocation()
{
    return m_isSelfSet;
}

//!
uint32_t IVTDataSource::getBaseAddress()
{
    return m_ivt.self;
}

bool IVTDataSource::setFieldByName(const std::string & name, uint32_t value)
{
    if (name == "entry")
    {
        m_ivt.entry = ENDIAN_HOST_TO_LITTLE_U32(value);
    }
    else if (name == "dcd")
    {
        m_ivt.dcd = ENDIAN_HOST_TO_LITTLE_U32(value);
    }
    else if (name == "boot_data")
    {
        m_ivt.boot_data = ENDIAN_HOST_TO_LITTLE_U32(value);
    }
    else if (name == "self")
    {
        m_ivt.self = ENDIAN_HOST_TO_LITTLE_U32(value);
        m_isSelfSet = true;
    }
    else if (name == "csf")
    {
        m_ivt.csf = ENDIAN_HOST_TO_LITTLE_U32(value);
    }
    else
    {
        // Unrecognized field name.
        return false;
    }
    
    return true;
}