Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #33

Merged
merged 3 commits into from
Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions uxmhf-rpi3/uobjcoll/uobjs/main/include/picar-s.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,28 @@
#ifndef __ASSEMBLY__

typedef struct {
// Input parameters
uint8_t len;
int array[5];
int speed;
int turn_angle;
int step;
} input_params;

typedef struct {
/* Output parameters */
int out_speed;
int out_turning_angle;
int out_step;
} output_params;

typedef struct {
/* All parameters */
uint32_t encrypted_buffer_va;
uint32_t decrypted_buffer_va;
uint8_t len;
uint32_t buffer_va;
uint32_t buffer_va;
uint32_t in_params_va;
uint32_t out_params_va;
}picar_s_param_t;

#endif // __ASSEMBLY__
Expand Down
117 changes: 104 additions & 13 deletions uxmhf-rpi3/uobjcoll/uobjs/main/uapps/uapp-picar-s.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

/*
picar-s hypapp
guest picar-s test
guest picar-s test
author: amit vasudevan (amitvasudevan@acm.org)
*/

Expand Down Expand Up @@ -63,11 +63,89 @@ __attribute__((section(".data"))) bool uapp_picar_s_activated=false;

#define UHSIGN_KEY_SIZE (sizeof(uhsign_key_picar))
#define HMAC_DIGEST_SIZE 32
#define NUM_REF 5


/* Private Functions */
void calculate_speed(int *array,int arr_len,int fw_speed,int *sp, int *st){
const int a_step = 2;
const int b_step = 8;
const int c_step = 24;
const int d_step = 40;
if((array[0] == 0) && (array[1] == 0) && (array[2] == 1) &&
(array[3] == 0) && (array[4] == 0) ){
*st = 0;
}
else if(((array[0] == 0) && (array[1] == 1) && (array[2] == 1) &&
(array[3] == 0) && (array[4] == 0) ) ||
((array[0] == 0) && (array[1] == 0) && (array[2] == 1) &&
(array[3] == 1) && (array[4] == 0))) {
*st = a_step;
*sp = fw_speed - 10;
}
else if(((array[0] == 0) && (array[1] == 1) && (array[2] == 0) &&
(array[3] == 0) && (array[4] == 0) ) ||
((array[0] == 0) && (array[1] == 0) && (array[2] == 0) &&
(array[3] == 1) && (array[4] == 0))) {
*st = b_step;
*sp = fw_speed - 15;
}
else if(((array[0] == 1) && (array[1] == 1) && (array[2] == 0) &&
(array[3] == 0) && (array[4] == 0) ) ||
((array[0] == 0) && (array[1] == 0) && (array[2] == 0) &&
(array[3] == 1) && (array[4] == 1))) {
*st = c_step;
*sp = fw_speed - 25;
}
else if(((array[0] == 1) && (array[1] == 0) && (array[2] == 0) &&
(array[3] == 0) && (array[4] == 0) ) ||
((array[0] == 0) && (array[1] == 0) && (array[2] == 0) &&
(array[3] == 0) && (array[4] == 1))) {
*st = d_step;
*sp = fw_speed - 35;
}
else{
*st = d_step;
*sp = fw_speed - 40;
}
}


void calculate_angle(int *array,int arr_len,int *turn_angle, int st){
if((array[0] == 0) && (array[1] == 0) && (array[2] == 1) &&
(array[3] == 0) && (array[4] == 0) ){
*turn_angle = 90;
}
else if(((array[0] == 0) && (array[1] == 1) && (array[2] == 1) &&
(array[3] == 0) && (array[4] == 0) ) ||
((array[0] == 0) && (array[1] == 1) && (array[2] == 0) &&
(array[3] == 0) && (array[4] == 0)) ||
((array[0] == 1) && (array[1] == 1) && (array[2] == 0) &&
(array[3] == 0) && (array[4] == 0)) ||
((array[0] == 1) && (array[1] == 0) && (array[2] == 0) &&
(array[3] == 0) && (array[4] == 0)) ) {
*turn_angle = (int)(90 - st);
}
else if(((array[0] == 0) && (array[1] == 0) && (array[2] == 1) &&
(array[3] == 1) && (array[4] == 0) ) ||
((array[0] == 0) && (array[1] == 0) && (array[2] == 0) &&
(array[3] == 1) && (array[4] == 0)) ||
((array[0] == 0) && (array[1] == 0) && (array[2] == 0) &&
(array[3] == 1) && (array[4] == 1)) ||
((array[0] == 0) && (array[1] == 0) && (array[2] == 0) &&
(array[3] == 0) && (array[4] == 1)) ) {
*turn_angle = (int)(90 + st);
}
}



bool uapp_picar_s_handlehcall(u32 picar_s_function, void *picar_s_buffer, u32 picar_s_buffer_len){
picar_s_param_t *upicar;
unsigned long digest_size = HMAC_DIGEST_SIZE;
upicar = (picar_s_param_t *)picar_s_buffer;
input_params *in_params;
output_params *out_params;

if(picar_s_function == UAPP_PICAR_S_FUNCTION_PROT) {

Expand All @@ -82,27 +160,41 @@ bool uapp_picar_s_handlehcall(u32 picar_s_function, void *picar_s_buffer, u32 pi
}else if (picar_s_function == UAPP_PICAR_S_FUNCTION_TEST){
uint32_t encrypted_buffer_pa;
uint32_t decrypted_buffer_pa;
uint32_t in_params_pa;
uint32_t out_params_pa;

/* _XDPRINTFSMP_("%s: Got control: encrypted_buffer_va=0x%08x, decrypted_buffer_va=0x%08x\n",
/* _XDPRINTFSMP_("%s: Got control: encrypted_buffer_va=0x%08x, decrypted_buffer_va=0x%08x\n",
__func__, upicar->encrypted_buffer_va, upicar->decrypted_buffer_va);
*/

if(!uapp_va2pa(upicar->encrypted_buffer_va, &encrypted_buffer_pa) ||
!uapp_va2pa(upicar->decrypted_buffer_va, &decrypted_buffer_pa) ){
!uapp_va2pa(upicar->decrypted_buffer_va, &decrypted_buffer_pa) ||
!uapp_va2pa(upicar->in_params_va, &in_params_pa) ||
!uapp_va2pa(upicar->out_params_va, &out_params_pa))
{
//error, this should not happen, probably need to print a message to serial debug and halt
_XDPRINTFSMP_("%s: Error, could not translate va2pa!\n", __func__);
return false;

}else{

int out_step;
int out_speed;
int out_turning_angle;
in_params = (input_params *) in_params_pa;
out_params = (output_params *) out_params_pa;
/* _XDPRINTFSMP_("%s: encrypted buffer va=0x%08x, pa=0x%08x\n", __func__,
upicar->encrypted_buffer_va, encrypted_buffer_pa);
upicar->encrypted_bu:179ffer_va, encrypted_buffer_pa);

_XDPRINTFSMP_("%s: decrypted buffer va=0x%08x, pa=0x%08x\n", __func__,
upicar->decrypted_buffer_va, decrypted_buffer_pa);
*/
uberspark_uobjrtl_crypto__mac_hmacsha256__hmac_sha256_memory (uhsign_key_picar, (unsigned long) UHSIGN_KEY_SIZE, (unsigned char *) encrypted_buffer_pa, (unsigned long) upicar->len, decrypted_buffer_pa, &digest_size);

}
uberspark_uobjrtl_crypto__mac_hmacsha256__hmac_sha256_memory (uhsign_key_picar, (unsigned long) UHSIGN_KEY_SIZE, (unsigned char *) encrypted_buffer_pa, (unsigned long) in_params->len, decrypted_buffer_pa, &digest_size);
calculate_speed(in_params->array,NUM_REF,in_params->speed,&out_speed,&out_step);
calculate_angle(in_params->array,NUM_REF,&out_turning_angle,out_step);
out_params->out_speed = out_speed;
out_params->out_step = out_step;
out_params->out_turning_angle = out_turning_angle;
}

return true;

Expand All @@ -116,21 +208,21 @@ void uapp_picar_s_handlehcall_prot(picar_s_param_t *upicar){
u64 roattrs;
uint32_t buffer_pa;
u64 prot;


#if defined (__UBERSPARK_UOBJCOLL_CONFIGDEF_ENABLE_UART_PL011__) || defined (__UBERSPARK_UOBJCOLL_CONFIGDEF_ENABLE_UART_MINI__)
//initialize uart
uart_init();
#endif

_XDPRINTFSMP_("%s: Got control: buffer_va=0x%08x...\n", __func__, upicar->buffer_va);

if(!uapp_va2pa(upicar->buffer_va, &buffer_pa)){
//error, this should not happen, probably need to print a message to serial debug and halt
_XDPRINTFSMP_("%s: Error, could not translate va2pa!\n", __func__);

}else{
roattrs =
roattrs =
(LDESC_S2_MC_OUTER_WRITE_BACK_CACHEABLE_INNER_WRITE_BACK_CACHEABLE << LDESC_S2_MEMATTR_MC_SHIFT) |
(LDESC_S2_S2AP_READ_ONLY << LDESC_S2_MEMATTR_S2AP_SHIFT) |
(MEM_INNER_SHAREABLE << LDESC_S2_MEMATTR_SH_SHIFT) |
Expand Down Expand Up @@ -163,7 +255,7 @@ void uapp_picar_s_handlehcall_unprot(picar_s_param_t *upicar){
_XDPRINTFSMP_("%s: Error, could not translate va2pa!\n", __func__);

}else{
rwattrs =
rwattrs =
(LDESC_S2_MC_OUTER_WRITE_BACK_CACHEABLE_INNER_WRITE_BACK_CACHEABLE << LDESC_S2_MEMATTR_MC_SHIFT) |
(LDESC_S2_S2AP_READ_WRITE << LDESC_S2_MEMATTR_S2AP_SHIFT) |
(MEM_INNER_SHAREABLE << LDESC_S2_MEMATTR_SH_SHIFT) |
Expand All @@ -178,4 +270,3 @@ void uapp_picar_s_handlehcall_unprot(picar_s_param_t *upicar){

}
}