mirror of
https://gitea.osmocom.org/sim-card/simtrace2.git
synced 2026-03-19 14:58:35 +03:00
Change directory structure to align with Atmel softpack
This way we can easily check with 'diff' for differences in our code and Atmel softpack. Also, this layout is more suitable for building various different firmware images (e.g. factory-test, dfu-loader, main application) for a variety of different boards (simtrace, owhw, qmod).
This commit is contained in:
64
firmware/libcommon/source/iso7816_fidi.c
Normal file
64
firmware/libcommon/source/iso7816_fidi.c
Normal file
@@ -0,0 +1,64 @@
|
||||
/* ISO7816-3 Fi/Di tables + computation */
|
||||
/* (C) 2010-2015 by Harald Welte <hwelte@hmw-consulting.de>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "utils.h"
|
||||
#include "iso7816_fidi.h"
|
||||
|
||||
/* Table 7 of ISO 7816-3:2006 */
|
||||
static const uint16_t fi_table[] = {
|
||||
372, 372, 558, 744, 1116, 1488, 1860, 0,
|
||||
0, 512, 768, 1024, 1536, 2048, 0, 0
|
||||
};
|
||||
|
||||
/* Table 8 from ISO 7816-3:2006 */
|
||||
static const uint8_t di_table[] = {
|
||||
0, 1, 2, 4, 8, 16, 32, 64,
|
||||
12, 20, 2, 4, 8, 16, 32, 64,
|
||||
};
|
||||
|
||||
/* compute the F/D ratio based on Fi and Di values */
|
||||
int compute_fidi_ratio(uint8_t fi, uint8_t di)
|
||||
{
|
||||
uint16_t f, d;
|
||||
int ret;
|
||||
|
||||
if (fi >= ARRAY_SIZE(fi_table) ||
|
||||
di >= ARRAY_SIZE(di_table))
|
||||
return -EINVAL;
|
||||
|
||||
f = fi_table[fi];
|
||||
if (f == 0)
|
||||
return -EINVAL;
|
||||
|
||||
d = di_table[di];
|
||||
if (d == 0)
|
||||
return -EINVAL;
|
||||
|
||||
/* See table 7 of ISO 7816-3: From 1000 on we divide by 1/d,
|
||||
* which equals a multiplication by d */
|
||||
if (di < 8)
|
||||
ret = f / d;
|
||||
else
|
||||
ret = f * d;
|
||||
|
||||
return ret;
|
||||
}
|
||||
139
firmware/libcommon/source/req_ctx.c
Normal file
139
firmware/libcommon/source/req_ctx.c
Normal file
@@ -0,0 +1,139 @@
|
||||
/* USB Request Context for OpenPCD / OpenPICC / SIMtrace
|
||||
* (C) 2006-2016 by Harald Welte <hwelte@hmw-consulting.de>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
*/
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "utils.h"
|
||||
#include "trace.h"
|
||||
#include "req_ctx.h"
|
||||
|
||||
#define NUM_RCTX_SMALL 10
|
||||
#define NUM_RCTX_LARGE 0
|
||||
|
||||
#define NUM_REQ_CTX (NUM_RCTX_SMALL+NUM_RCTX_LARGE)
|
||||
|
||||
static uint8_t rctx_data[NUM_RCTX_SMALL][RCTX_SIZE_SMALL];
|
||||
static uint8_t rctx_data_large[NUM_RCTX_LARGE][RCTX_SIZE_LARGE];
|
||||
|
||||
static struct req_ctx req_ctx[NUM_REQ_CTX];
|
||||
|
||||
struct req_ctx __ramfunc *
|
||||
req_ctx_find_get(int large, uint32_t old_state, uint32_t new_state)
|
||||
{
|
||||
struct req_ctx *toReturn = NULL;
|
||||
unsigned long flags;
|
||||
unsigned int i;
|
||||
|
||||
if (old_state >= RCTX_STATE_COUNT || new_state >= RCTX_STATE_COUNT) {
|
||||
TRACE_DEBUG("Invalid parameters for req_ctx_find_get\r\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
local_irq_save(flags);
|
||||
for (i = 0; i < ARRAY_SIZE(req_ctx); i++) {
|
||||
if (req_ctx[i].state == old_state) {
|
||||
toReturn = &req_ctx[i];
|
||||
toReturn->state = new_state;
|
||||
break;
|
||||
}
|
||||
}
|
||||
local_irq_restore(flags);
|
||||
|
||||
TRACE_DEBUG("%s(%u, %u, %u)=%p\r\n", __func__, large, old_state,
|
||||
new_state, toReturn);
|
||||
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
uint8_t req_ctx_num(struct req_ctx *ctx)
|
||||
{
|
||||
return ctx - req_ctx;
|
||||
}
|
||||
|
||||
void req_ctx_set_state(struct req_ctx *ctx, uint32_t new_state)
|
||||
{
|
||||
unsigned long flags;
|
||||
|
||||
TRACE_DEBUG("%s(ctx=%p, new_state=%u)\r\n", __func__, ctx, new_state);
|
||||
|
||||
if (new_state >= RCTX_STATE_COUNT) {
|
||||
TRACE_DEBUG("Invalid new_state for req_ctx_set_state\r\n");
|
||||
return;
|
||||
}
|
||||
local_irq_save(flags);
|
||||
ctx->state = new_state;
|
||||
local_irq_restore(flags);
|
||||
}
|
||||
|
||||
#ifdef DEBUG_REQCTX
|
||||
void req_print(int state) {
|
||||
int count = 0;
|
||||
struct req_ctx *ctx, *last = NULL;
|
||||
DEBUGP("State [%02i] start <==> ", state);
|
||||
ctx = req_ctx_queues[state];
|
||||
while (ctx) {
|
||||
if (last != ctx->prev)
|
||||
DEBUGP("*INV_PREV* ");
|
||||
DEBUGP("%08X => ", ctx);
|
||||
last = ctx;
|
||||
ctx = ctx->next;
|
||||
count++;
|
||||
if (count > NUM_REQ_CTX) {
|
||||
DEBUGP("*WILD POINTER* => ");
|
||||
break;
|
||||
}
|
||||
}
|
||||
TRACE_DEBUG("NULL");
|
||||
if (!req_ctx_queues[state] && req_ctx_tails[state]) {
|
||||
TRACE_DEBUG("NULL head, NON-NULL tail\r\n");
|
||||
}
|
||||
if (last != req_ctx_tails[state]) {
|
||||
TRACE_DEBUG("Tail does not match last element\r\n");
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
void req_ctx_put(struct req_ctx *ctx)
|
||||
{
|
||||
return req_ctx_set_state(ctx, RCTX_S_FREE);
|
||||
}
|
||||
|
||||
void req_ctx_init(void)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < NUM_RCTX_SMALL; i++) {
|
||||
req_ctx[i].size = RCTX_SIZE_SMALL;
|
||||
req_ctx[i].tot_len = 0;
|
||||
req_ctx[i].data = rctx_data[i];
|
||||
req_ctx[i].state = RCTX_S_FREE;
|
||||
TRACE_DEBUG("SMALL req_ctx[%02i] initialized at %p, Data: %p => %p\r\n",
|
||||
i, req_ctx + i, req_ctx[i].data, req_ctx[i].data + RCTX_SIZE_SMALL);
|
||||
}
|
||||
|
||||
for (; i < NUM_REQ_CTX; i++) {
|
||||
req_ctx[i].size = RCTX_SIZE_LARGE;
|
||||
req_ctx[i].tot_len = 0;
|
||||
req_ctx[i].data = rctx_data_large[i];
|
||||
req_ctx[i].state = RCTX_S_FREE;
|
||||
TRACE_DEBUG("LARGE req_ctx[%02i] initialized at %p, Data: %p => %p\r\n",
|
||||
i, req_ctx + i, req_ctx[i].data, req_ctx[i].data + RCTX_SIZE_LARGE);
|
||||
}
|
||||
}
|
||||
70
firmware/libcommon/source/ringbuffer.c
Normal file
70
firmware/libcommon/source/ringbuffer.c
Normal file
@@ -0,0 +1,70 @@
|
||||
#include "ringbuffer.h"
|
||||
#include "trace.h"
|
||||
#include "utils.h"
|
||||
|
||||
void rbuf_reset(volatile ringbuf * rb)
|
||||
{
|
||||
unsigned long state;
|
||||
|
||||
local_irq_save(state);
|
||||
rb->ird = 0;
|
||||
rb->iwr = 0;
|
||||
local_irq_restore(state);
|
||||
}
|
||||
|
||||
uint8_t rbuf_read(volatile ringbuf * rb)
|
||||
{
|
||||
unsigned long state;
|
||||
uint8_t val;
|
||||
|
||||
local_irq_save(state);
|
||||
val = rb->buf[rb->ird];
|
||||
rb->ird = (rb->ird + 1) % RING_BUFLEN;
|
||||
local_irq_restore(state);
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
uint8_t rbuf_peek(volatile ringbuf * rb)
|
||||
{
|
||||
return rb->buf[rb->ird];
|
||||
}
|
||||
|
||||
bool rbuf_is_empty(volatile ringbuf * rb)
|
||||
{
|
||||
return rb->ird == rb->iwr;
|
||||
}
|
||||
|
||||
static bool __rbuf_is_full(volatile ringbuf * rb)
|
||||
{
|
||||
return rb->ird == (rb->iwr + 1) % RING_BUFLEN;
|
||||
}
|
||||
|
||||
bool rbuf_is_full(volatile ringbuf * rb)
|
||||
{
|
||||
unsigned long state;
|
||||
bool rc;
|
||||
|
||||
local_irq_save(state);
|
||||
rc = rb->ird == (rb->iwr + 1) % RING_BUFLEN;
|
||||
local_irq_restore(state);
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
void rbuf_write(volatile volatile ringbuf * rb, uint8_t item)
|
||||
{
|
||||
unsigned long state;
|
||||
|
||||
local_irq_save(state);
|
||||
if (!__rbuf_is_full(rb)) {
|
||||
rb->buf[rb->iwr] = item;
|
||||
rb->iwr = (rb->iwr + 1) % RING_BUFLEN;
|
||||
local_irq_restore(state);
|
||||
} else {
|
||||
local_irq_restore(state);
|
||||
TRACE_ERROR("Ringbuffer full, losing bytes!");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
138
firmware/libcommon/source/syscalls.c
Normal file
138
firmware/libcommon/source/syscalls.c
Normal file
@@ -0,0 +1,138 @@
|
||||
/* ----------------------------------------------------------------------------
|
||||
* ATMEL Microcontroller Software Support
|
||||
* ----------------------------------------------------------------------------
|
||||
* Copyright (c) 2009, Atmel Corporation
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the disclaimer below.
|
||||
*
|
||||
* Atmel's name may not be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
*
|
||||
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
* ----------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
/**
|
||||
* \file syscalls.c
|
||||
*
|
||||
* Implementation of newlib syscall.
|
||||
*
|
||||
*/
|
||||
|
||||
/*----------------------------------------------------------------------------
|
||||
* Headers
|
||||
*----------------------------------------------------------------------------*/
|
||||
|
||||
|
||||
#include "board.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
/*----------------------------------------------------------------------------
|
||||
* Exported variables
|
||||
*----------------------------------------------------------------------------*/
|
||||
|
||||
#undef errno
|
||||
extern int errno ;
|
||||
extern int _end ;
|
||||
|
||||
/*----------------------------------------------------------------------------
|
||||
* Exported functions
|
||||
*----------------------------------------------------------------------------*/
|
||||
extern void _exit( int status ) ;
|
||||
extern void _kill( int pid, int sig ) ;
|
||||
extern int _getpid ( void ) ;
|
||||
|
||||
extern caddr_t _sbrk ( int incr )
|
||||
{
|
||||
static unsigned char *heap = NULL ;
|
||||
unsigned char *prev_heap ;
|
||||
|
||||
if ( heap == NULL )
|
||||
{
|
||||
heap = (unsigned char *)&_end ;
|
||||
}
|
||||
prev_heap = heap;
|
||||
|
||||
heap += incr ;
|
||||
|
||||
return (caddr_t) prev_heap ;
|
||||
}
|
||||
|
||||
extern int link( char *old, char *new )
|
||||
{
|
||||
return -1 ;
|
||||
}
|
||||
|
||||
extern int _close( int file )
|
||||
{
|
||||
return -1 ;
|
||||
}
|
||||
|
||||
extern int _fstat( int file, struct stat *st )
|
||||
{
|
||||
st->st_mode = S_IFCHR ;
|
||||
|
||||
return 0 ;
|
||||
}
|
||||
|
||||
extern int _isatty( int file )
|
||||
{
|
||||
return 1 ;
|
||||
}
|
||||
|
||||
extern int _lseek( int file, int ptr, int dir )
|
||||
{
|
||||
return 0 ;
|
||||
}
|
||||
|
||||
extern int _read(int file, char *ptr, int len)
|
||||
{
|
||||
return 0 ;
|
||||
}
|
||||
|
||||
extern int _write( int file, char *ptr, int len )
|
||||
{
|
||||
int iIndex ;
|
||||
|
||||
for ( iIndex=0 ; iIndex < len ; iIndex++, ptr++ )
|
||||
{
|
||||
UART_PutChar( *ptr ) ;
|
||||
}
|
||||
return iIndex ;
|
||||
}
|
||||
|
||||
extern void _exit( int status )
|
||||
{
|
||||
printf( "Exiting with status %d.\n", status ) ;
|
||||
|
||||
for ( ; ; ) ;
|
||||
}
|
||||
|
||||
extern void _kill( int pid, int sig )
|
||||
{
|
||||
return ;
|
||||
}
|
||||
|
||||
extern int _getpid ( void )
|
||||
{
|
||||
return -1 ;
|
||||
}
|
||||
Reference in New Issue
Block a user