1/*  *********************************************************************
2    *  Broadcom Common Firmware Environment (CFE)
3    *
4    *  Memory mangling functions		File: lib_memfuncs.c
5    *
6    *  Routines that the user interface commands use to modify
7    *  memory (peek, poke, etc.)  Most are wrapped in exception
8    *  handler clothing.
9    *
10    *  Author:  Binh Vo, Mitch Lichtenberg
11    *
12    *********************************************************************
13    *
14    *  Copyright 2000,2001,2002,2003
15    *  Broadcom Corporation. All rights reserved.
16    *
17    *  This software is furnished under license and may be used and
18    *  copied only in accordance with the following terms and
19    *  conditions.  Subject to these conditions, you may download,
20    *  copy, install, use, modify and distribute modified or unmodified
21    *  copies of this software in source and/or binary form.  No title
22    *  or ownership is transferred hereby.
23    *
24    *  1) Any source code used, modified or distributed must reproduce
25    *     and retain this copyright notice and list of conditions
26    *     as they appear in the source file.
27    *
28    *  2) No right is granted to use any trade name, trademark, or
29    *     logo of Broadcom Corporation.  The "Broadcom Corporation"
30    *     name may not be used to endorse or promote products derived
31    *     from this software without the prior written permission of
32    *     Broadcom Corporation.
33    *
34    *  3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR
35    *     IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED
36    *     WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
37    *     PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT
38    *     SHALL BROADCOM BE LIABLE FOR ANY DAMAGES WHATSOEVER, AND IN
39    *     PARTICULAR, BROADCOM SHALL NOT BE LIABLE FOR DIRECT, INDIRECT,
40    *     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
41    *     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
42    *     GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43    *     BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
44    *     OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
45    *     TORT (INCLUDING NEGLIGENCE OR OTHERWISE), EVEN IF ADVISED OF
46    *     THE POSSIBILITY OF SUCH DAMAGE.
47    ********************************************************************* */
48
49#include "cfe.h"
50#include "lib_hssubr.h"
51#include "lib_try.h"
52#include "lib_memfuncs.h"
53
54
55
56/*  *********************************************************************
57    *  mem_peek(d,addr,type)
58    *
59    *  Read memory of the specified type at the specified address.
60    *  Exceptions are caught in the case of a bad memory reference.
61    *
62    *  Input parameters:
63    *  	   d - pointer to where data should be placed
64    *  	   addr - address to read
65    *  	   type - type of read to do (MEM_BYTE, etc.)
66    *
67    *  Return value:
68    *  	   0 if ok
69    *  	   else error code
70    ********************************************************************* */
71
72int mem_peek(void *d, hsaddr_t addr, int type)
73{
74
75    jmpbuf_t *jb;
76
77    jb = exc_initialize_block();
78    if( jb == NULL ) {
79	return CFE_ERR_NOMEM;
80	}
81
82    if (exc_try(jb) == 0) {
83
84	switch (type) {
85	    case MEM_BYTE:
86		*(uint8_t *)d = hs_read8(addr);
87		break;
88	    case MEM_HALFWORD:
89		*(uint16_t *)d = hs_read16(addr);
90		break;
91	    case MEM_WORD:
92		*(uint32_t *)d = hs_read32(addr);
93		break;
94	    case MEM_QUADWORD:
95		*(uint64_t *)d = hs_read64(addr);
96		break;
97	    default:
98		return CFE_ERR_INV_PARAM;
99	    }
100
101	exc_cleanup_block(jb);
102	}
103    else {
104	/*Exception handler*/
105
106	exc_cleanup_handler(jb, EXC_NORMAL_RETURN);
107	return CFE_ERR_GETMEM;
108	}
109
110    return 0;
111}
112
113/* *********************************************************************
114   *  Write memory of type at address addr with value val.
115   *  Exceptions are caught, handled (error message) and function
116   *  returns with 0.
117   *
118   *  1 success
119   *  0 failure
120   ********************************************************************* */
121
122int mem_poke(hsaddr_t addr, uint64_t val, int type)
123{
124
125    jmpbuf_t *jb;
126
127    jb = exc_initialize_block();
128    if( jb == NULL ) {
129	return CFE_ERR_NOMEM;
130	}
131
132    if (exc_try(jb) == 0) {
133
134	switch (type) {
135	    case MEM_BYTE:
136		hs_write8(addr,(uint8_t)val);
137		break;
138	    case MEM_HALFWORD:
139		hs_write16(addr,(uint16_t)val);
140		break;
141	    case MEM_WORD:
142		hs_write32(addr,(uint32_t)val);
143		break;
144	    case MEM_QUADWORD:
145		hs_write64(addr,(uint64_t)val);
146		break;
147	    default:
148		return CFE_ERR_INV_PARAM;
149	    }
150
151	exc_cleanup_block(jb);
152	}
153    else {
154	/*Exception handler*/
155
156	exc_cleanup_handler(jb, EXC_NORMAL_RETURN);
157	return CFE_ERR_SETMEM;
158	}
159
160    return 0;
161}
162
163
164
165
166