int main (void) { struct xpr z, h, f, u; printf (" Test of Exp Functions\n"); z = xneg (xOne); h = atox ("0.5"); u = atox ("3.01"); for (; xprcmp (&z, &u) < 0; z = xadd (z, h, 0)) { /* compute extended precision exponential */ f = xexp (z); printf (" %8.4f ", xtodbl (z)); xprxpr (f, decd); f = xexp2 (z); printf ("\n "); xprxpr (f, decd); f = xexp10 (z); printf ("\n "); xprxpr (f, decd); putchar ('\n'); } return 0; }
int main (void) { struct xpr z, w, f, u; int k; char cf[3]; cf[0] = 's'; cf[1] = 'c'; cf[2] = 't'; for (k = 0; k < 3; ++k) { switch (cf[k]) { case 't': printf (" Test of Atanh Function\n"); break; case 's': printf (" Test of Asinh Function\n"); break; case 'c': printf (" Test of Acosh Function\n"); break; } z = atox ("-3.10"); w = atox ("0.2"); u = atox ("3"); for (; xprcmp (&z, &u) < 0; z = xadd (z, w, 0)) { xErrNo = 0; /* compute selected extended precision hyperbolic function */ switch (cf[k]) { case 't': f = xatanh (z); break; case 's': f = xasinh (z); break; case 'c': f = xacosh (z); break; } if (!xErrNo) { printf (" %8.4f ", xtodbl (z)); xprxpr (f, decd); putchar ('\n'); } else printf ("*** Out of range\n"); } } return 0; }
/** * \brief Build a SansgridSquawk from entries in the dictionary * \param[in] dict Populated Dictionary Structure * \param[in] size Number of entries in the Dictionary * \param[out] sg_serial Outputted SansgridSerial Structure * * Note that sg_serial must point to a valid chunk of memory. * \returns * The Device Identifier rdid is returned when found. \n * If the rdid is missing, return -1. */ static int8_t convertSquawk(Dictionary dict[], int size, SansgridSerial *sg_serial) { // Get a squawk datatype from the payload SansgridSquawk sg_squawk; atox(&sg_squawk.datatype, match(dict, size, "dt"), 1*sizeof(uint8_t)); atox(sg_squawk.data, match(dict, size, "data"), 80*sizeof(uint8_t)); memcpy(sg_serial->payload, &sg_squawk, sizeof(SansgridSquawk)); return translateRdid(dict, size); }
/** * \brief Build a SansgridMock from entries in the dictionary * \param[in] dict Populated Dictionary Structure * \param[in] size Number of entries in the Dictionary * \param[out] sg_serial Outputted SansgridSerial Structure * * Note that sg_serial must point to a valid chunk of memory. * \returns * The Device Identifier rdid is returned when found. \n * If the rdid is missing, return -1. */ static int8_t convertMock(Dictionary dict[], int size, SansgridSerial *sg_serial) { // Get a mock datatype from the payload SansgridMock sg_mock; atox(&sg_mock.datatype, match(dict, size, "dt"), 1*sizeof(uint8_t)); atox(sg_mock.pubkey, match(dict, size, "senspubkey"),80*sizeof(uint8_t)); memcpy(sg_serial->payload, &sg_mock, sizeof(SansgridMock)); return translateRdid(dict, size); }
/** * \brief Build a SansgridSing from entries in the dictionary * \param[in] dict Populated Dictionary Structure * \param[in] size Number of entries in the Dictionary * \param[out] sg_serial Outputted SansgridSerial Structure * * Note that sg_serial must point to a valid chunk of memory. * \returns * The Device Identifier rdid is returned when found. \n * If the rdid is missing, return -1. */ static int8_t convertSing(Dictionary dict[], int size, SansgridSerial *sg_serial) { // Get a sing datatype from the payload SansgridSing sg_sing; atox(&sg_sing.datatype, match(dict, size, "dt"), 1*sizeof(uint8_t)); atox(sg_sing.pubkey, match(dict, size, "servpubkey"),80*sizeof(uint8_t)); memcpy(sg_serial->payload, &sg_sing, sizeof(SansgridSing)); return translateRdid(dict, size); }
/** * \brief Build a SansgridChirp from entries in the dictionary * \param[in] dict Populated Dictionary Structure * \param[in] size Number of entries in the Dictionary * \param[out] sg_serial Outputted SansgridSerial Structure * * Note that sg_serial must point to a valid chunk of memory. * \returns * The Device Identifier rdid is returned when found. \n * If the rdid is missing, return -1. */ static int8_t convertChirp(Dictionary dict[], int size, SansgridSerial *sg_serial) { // Get a chirp datatype from the payload SansgridChirp sg_chirp; atox(&sg_chirp.datatype, match(dict, size, "dt"), 1*sizeof(uint8_t)); atox(&sg_chirp.sid, match(dict, size, "sid"), 1*sizeof(uint8_t)); strncpy((char*)sg_chirp.data, match(dict, size, "data"), 79); //atox(sg_chirp.data, match(dict, size, "data"), 79*sizeof(uint8_t)); memcpy(sg_serial->payload, &sg_chirp, sizeof(SansgridChirp)); return translateRdid(dict, size); }
static int fips_asc_check(const unsigned char *sig, const char *asc_sig) { char tsig[20]; const char *p; int i; if (strlen(asc_sig) != 40) return 0; for (i = 0, p = asc_sig; i < 20; i++, p += 2) tsig[i] = (atox(p[0]) << 4) | atox(p[1]); if (memcmp(tsig, sig, 20)) return 0; return 1; }
int main (int na, char **av) { struct xpr s, t, f; char nbx[64], nby[64]; FILE *fp; if (na != 2) { printf ("para: input_file\n"); exit (-1); } fp = fopen (*++av, "r"); printf (" Test of Elementary Operations\n"); while (fscanf (fp, "%s %s", nbx, nby) != EOF) { s = atox (nbx); t = atox (nby); printf ("\n x= "); xprxpr (s, decd); printf ("\n y= "); xprxpr (t, decd); /* extended precision addition */ printf ("\nadd\n"); f = xadd (s, t, 0); printf (" %16.10f\n", xtodbl (f)); xprxpr (f, decd); /* extended precision subtraction */ printf ("\nsubtract\n"); f = xadd (s, t, 1); printf (" %16.10f\n", xtodbl (f)); xprxpr (f, decd); /* extended precision multiplication */ printf ("\nmultiply\n"); f = xmul (s, t); printf (" %16.10f\n", xtodbl (f)); xprxpr (f, decd); /* extended precision division */ printf ("\ndivide\n"); f = xdiv (s, t); printf (" %16.10f\n", xtodbl (f)); xprxpr (f, decd); } putchar ('\n'); return 0; }
int main(void) { char *s = "0x12345678"; int n = atox(s); printf("n = %x\n", n); return 0; }
void UserPalette::addColor(unsigned type, unsigned rgb, Palette::GradientType grad, const std::string &text, int delay) { const unsigned maxType = sizeof(ColorTypeNames) / sizeof(ColorTypeNames[0]); if (type >= maxType) return; const std::string &configName = ColorTypeNames[type]; char buffer[20]; sprintf(buffer, "0x%06x", rgb); const std::string rgbString = config.getValue(configName, std::string(buffer)); unsigned int rgbValue = 0; if (rgbString.length() == 8 && rgbString[0] == '0' && rgbString[1] == 'x') rgbValue = atox(rgbString); else rgbValue = atoi(rgbString.c_str()); gcn::Color trueCol = rgbValue; grad = static_cast<GradientType>(config.getValue(configName + "Gradient", static_cast<int>(grad))); delay = config.getValueInt(configName + "Delay", delay); mColors[type].set(type, trueCol, grad, delay); mColors[type].text = text; if (grad != STATIC) mGradVector.push_back(&mColors[type]); }
int scanf(const char * format,...) { typedef __builtin_va_list va_list; va_list scan; char input[10]={0}; va_start(scan,format); while(*format) { if(*format == '%') switch(*++format) { case 'd':; int *num = va_arg(scan,int*); read1(input,sizeof(int)); *num = atoi(input); break; case 's':; char *str = va_arg(scan,char*); read1(str,10); break; case 'x':; uint64_t *num1 = va_arg(scan,uint64_t*); read1(input,sizeof(int)); *num1 =atox(input); default:break; } format++; } return 0; }
/** * \brief Build a SansgridIRStatus from entries in the dictionary * \param[in] dict Populated Dictionary Structure * \param[in] size Number of entries in the Dictionary * \param[out] sg_serial Outputted SansgridSerial Structure * * Note that sg_serial must point to a valid chunk of memory. * \returns * The Device Identifier rdid is returned when found. \n * If the rdid is missing, return -1. */ static int8_t convertIRStatus( Dictionary dict[], int size, SansgridSerial *sg_serial) { // Get an router<-->server datatype from the payload SansgridIRStatus sg_irstatus; char *status = NULL; memset(&sg_irstatus, 0x0, sizeof(SansgridIRStatus)); atox(&sg_irstatus.datatype, match(dict, size, "dt"), 1*sizeof(uint8_t)); atox(sg_irstatus.rdid, match(dict, size, "rdid"), 1*sizeof(uint8_t)); if ((status = match(dict, size, "status")) != NULL) { strcpy(sg_irstatus.status, status); } memcpy(sg_serial->payload, &sg_irstatus, sizeof(SansgridIRStatus)); return translateRdid(dict, size); }
int main (void) { struct xpr z, w, f, u; printf (" Test of Sqrt Function\n"); z = xZero; w = atox ("0.2"); u = atox ("4.01"); for (; xprcmp (&z, &u) < 0; z = xadd (z, w, 0)) { /* compute extended precision square root */ f = xsqrt (z); printf (" %8.4f ", xtodbl (z)); xprxpr (f, decd); putchar ('\n'); } return 0; }
/** * \brief Build a SansgridNest from entries in the dictionary * \param[in] dict Populated Dictionary Structure * \param[in] size Number of entries in the Dictionary * \param[out] sg_serial Outputted SansgridSerial Structure * * Note that sg_serial must point to a valid chunk of memory. * \returns * The Device Identifier rdid is returned when found. \n * If the rdid is missing, return -1. */ static int8_t convertNest(Dictionary dict[], int size, SansgridSerial *sg_serial) { // Get a nest datatype from the payload SansgridNest sg_nest; atox(&sg_nest.datatype, match(dict, size, "dt"), 1*sizeof(uint8_t)); memset(sg_nest.padding, 0x0, 80*sizeof(uint8_t)); memcpy(sg_serial->payload, &sg_nest, sizeof(SansgridNest)); return translateRdid(dict, size); }
static void MyPresent(IPSocket *socket, char *request, int bytes) { char i=0; uint8_t plain[8]; uint8_t key[10]; uint8_t cipher[8]; char *text="HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" "<html><body><h2>PRESENT Encryption</h2><br>" "64bits plain, 80bits key, 64bits cipher<br>"; char *br="<br>"; // (void)request; (void)bytes; atox(request+21,plain,8); atox(request+40,key,10); IPWrite(socket, (uint8*)text, (int)strlen(text)); present(plain,key,cipher); for (i=0;i<8;i++) { IPWrite(socket, (uint8*)(xtoa(plain[i])+1), 1); IPWrite(socket, (uint8*)(xtoa(plain[i])), 1); } IPWrite(socket, (uint8*)br, (int)strlen(br)); for (i=0;i<10;i++) { IPWrite(socket, (uint8*)(xtoa(key[i])+1),1); IPWrite(socket, (uint8*)(xtoa(key[i])), 1); } IPWrite(socket, (uint8*)br, (int)strlen(br)); for (i=0;i<8;i++) { IPWrite(socket, (uint8*)(xtoa(cipher[i])+1), 1); IPWrite(socket, (uint8*)(xtoa(cipher[i])),1); } IPWrite(socket, (uint8*)br, (int)strlen(br)); IPClose(socket); }
static int HexToBinary (const char *in, char *out, int len) { int top, bottom; while (len > 0) { top = atox(in[0]); if (top == -1) return 0; bottom = atox(in[1]); if (bottom == -1) return 0; *out++ = (top << 4) | bottom; in += 2; len -= 2; } if (len) return 0; *out++ = '\0'; return 1; }
int atoi_x(const char *nptr) { if (nptr[0] == '0' && (nptr[1] == 'x' | nptr[1] == 'X')) { return atox(&nptr[2]); } else { return atoi(nptr); } return 0; }
/** * \brief Convert the full string of hex values into an array * * \param[out] hexarray An array of 8-bit values * \param[in] str A null-terminated string of ASCII hex values * \param[in] hexsize Number of hex values (not used) */ void atox(uint8_t *hexarray, char *str, uint32_t hexsize) { // convert the full string of hex values into an array uint32_t i_str, i_hex = 0; int increment = 0; char chunk[3]; uint32_t length; uint32_t uval; int32_t offset; for (i_hex=0; i_hex<hexsize; i_hex++) hexarray[i_hex] = 0x0; i_hex = 0; if (str == NULL) return; length = strlen(str); if ((length+1)/2 > hexsize) { syslog(LOG_DEBUG, "atox: truncating string %s", str); length = hexsize*2; } else if ((length+1)/2 < hexsize) { syslog(LOG_DEBUG, "atox: left-padding string %s", str); offset = hexsize - (length+1)/2; return atox(hexarray+offset, str, hexsize-offset); } //offset = hexsize - ((length+1)/2); for (i_str=0; i_str<length;) { if (((length ^ i_str) & 0x1) == 0x1) { // make sure we catch case of 0x123 // where we should parse as 0x1 0x23 chunk[0] = str[i_str]; chunk[1] = '\0'; //sscanf(&str[i], "%1s", chunk); increment = 1; } else { chunk[0] = str[i_str]; chunk[1] = str[i_str+1]; chunk[2] = '\0'; //sscanf(&str[i], "%2s", chunk); increment = 2; } sscanf(chunk, "%x", &uval); hexarray[i_hex++] = (uval & 0xff); i_str+=increment; } return; }
/** * \brief Build a SansgridPeacock from entries in the dictionary * \param[in] dict Populated Dictionary Structure * \param[in] size Number of entries in the Dictionary * \param[out] sg_serial Outputted SansgridSerial Structure * * Note that sg_serial must point to a valid chunk of memory. * \returns * The Device Identifier rdid is returned when found. \n * If the rdid is missing, return -1. */ static int8_t convertPeacock(Dictionary dict[], int size, SansgridSerial *sg_serial) { // Get a peacock datatype from the payload char *label = NULL; SansgridPeacock sg_peacock; atox(&sg_peacock.datatype, match(dict, size, "dt"), 1*sizeof(uint8_t)); atox(&sg_peacock.IO_A_id, match(dict, size, "sida"), 1*sizeof(uint8_t)); atox(&sg_peacock.IO_A_class,match(dict, size, "classa"), 1*sizeof(uint8_t)); atox(&sg_peacock.IO_A_direc,match(dict, size, "dira"), 1*sizeof(uint8_t)); if ((label = match(dict, size, "labela")) == NULL) { memset(sg_peacock.IO_A_label, 0x0, 30*sizeof(char)); } else { strncpy(sg_peacock.IO_A_label, label, 30); } if ((label = match(dict, size, "unitsa")) == NULL) { memset(sg_peacock.IO_A_units, 0x0, 6*sizeof(char)); } else { strncpy(sg_peacock.IO_A_units, label, 6); } atox(&sg_peacock.IO_B_id, match(dict, size, "sidb"), 1*sizeof(uint8_t)); atox(&sg_peacock.IO_B_class,match(dict, size, "classb"), 1*sizeof(uint8_t)); atox(&sg_peacock.IO_B_direc,match(dict, size, "dirb"), 1*sizeof(uint8_t)); if ((label = match(dict, size, "labelb")) == NULL) { memset(sg_peacock.IO_B_label, 0x0, 30*sizeof(char)); } else { strncpy(sg_peacock.IO_B_label, label, 30); } if ((label = match(dict, size, "unitsb")) == NULL) { memset(sg_peacock.IO_B_units, 0x0, 6*sizeof(char)); } else { strncpy(sg_peacock.IO_B_units, label, 6); } atox(&sg_peacock.additional_IO_needed, match(dict, size, "additional"), 1*sizeof(uint8_t)); sg_peacock.padding = 0x0; memcpy(sg_serial->payload, &sg_peacock, sizeof(SansgridPeacock)); return translateRdid(dict, size); }
uint64_t atox64(char *cPtr) { uint8_t ucbyte = 0; char char_byte[3] = {}; uint64_t ui64_data = 0; char *data = cPtr; int ii; uint8_t isOddNum = 0; if (*(data) == '\0') { return 0; } isOddNum = strlen(data) % 2; for (ii = 0; ii < 8; ii++) { if ((ii==0) && (isOddNum)) { strncat(char_byte, "0", 1); strncat(char_byte+1, data, 1); data += 1; } else { strncpy(char_byte, data, 2); data += 2; } ucbyte = atox(char_byte); ui64_data = ui64_data << 8; ui64_data |= ((uint64_t)ucbyte & 0x00000000000000ff); if (*(data) == '\0') { break; } ucbyte = 0; } OF_LOG_MSG(OF_LOG_MOD, OF_LOG_DEBUG, "returning2 %llx",ui64_data); return ui64_data; }
int main (int na, char **av) { struct xpr x; int i; char buffer[BUFF_SIZE]; struct xoutflags ofs; unsigned long n; FILE *fp; if (na != 2) { printf ("para: input_file\n"); exit (-1); } fp = fopen (*++av, "r"); printf (" Test of Special Printing Functions\n"); while (fgets (buffer, BUFF_SIZE, fp) != NULL) { for (i = 0; buffer[i] != '\0'; i++); if (i > 0 && buffer[i - 1] == '\n') buffer[i - 1] = '\0'; if (buffer[0] == '*') { getflags (buffer + 1, &ofs); printf ("* %hi %hi %hi %hi %c\n", ofs.notat, ofs.sf, ofs.mfwd, ofs.lim, ofs.padding); } else { printf ("buffer : %s\n", buffer); x = atox (buffer); printf ("\t\t <%2d>\n", xfout (stdout, ofs, x)); printf ("\t\t <%2d>\n", xout (ofs, x)); n = xsout (buffer, 20, ofs, x); printf ("%s\t\t <%2lu>\n", buffer, n); } } fclose (fp); return 0; }
void Palette::addColor(Palette::ColorType type, int rgb, Palette::GradientType grad, const std::string &text, char c, int delay) { const std::string &configName = ColorTypeNames[type]; char buffer[20]; sprintf(buffer, "0x%06x", rgb); const std::string rgbString = config.getValue(configName, std::string(buffer)); unsigned int rgbValue = 0; if (rgbString.length() == 8 && rgbString[0] == '0' && rgbString[1] == 'x') rgbValue = atox(rgbString); else rgbValue = atoi(rgbString.c_str()); gcn::Color trueCol = rgbValue; grad = (GradientType) config.getValue(configName + "Gradient", grad); delay = (int) config.getValue(configName + "Delay", delay); mColVector[type].set(type, trueCol, grad, text, c, delay); if (grad != STATIC) mGradVector.push_back(&mColVector[type]); }
/** * \brief Build a SansgridEyeball from entries in the dictionary * \param[in] dict Populated Dictionary Structure * \param[in] size Number of entries in the Dictionary * \param[out] sg_serial Outputted SansgridSerial Structure * * Note that sg_serial must point to a valid chunk of memory. * \returns * The Device Identifier rdid is returned when found. \n * If the rdid is missing, return -1. */ static int32_t convertEyeball(Dictionary dict[], int size, SansgridSerial *sg_serial) { // Get an eyeball datatype from the payload SansgridEyeball sg_eyeball; atox(&sg_eyeball.datatype, match(dict, size, "dt"), 1*sizeof(uint8_t)); atox(sg_eyeball.manid, match(dict, size, "manid"), 4*sizeof(uint8_t)); atox(sg_eyeball.modnum, match(dict, size, "modnum"),4*sizeof(uint8_t)); atox(sg_eyeball.serial_number, match(dict, size, "sn"), 8*sizeof(uint8_t)); atox(&sg_eyeball.profile, match(dict, size, "profile"),1*sizeof(uint8_t)); atox(&sg_eyeball.mode, match(dict, size, "mode"), 1*sizeof(uint8_t)); memset(sg_eyeball.padding, 0x0, 62*sizeof(uint8_t)); memcpy(sg_serial->payload, &sg_eyeball, sizeof(SansgridEyeball)); return translateRdid(dict, size); }
void SansgridRadio::setXBsn() { char xbsn_str[17]; uint8_t * cmdOut = new uint8_t[8]; memset(xbsn_str,'0',17); memset(cmdOut,0,8); xbsn_str[16] = '\0'; // get char sn value atCmd(cmdOut,"ATSH"); memcpy((xbsn_str+2),cmdOut,6); // hard code that sn starts with a 00 byte atCmd(cmdOut,"ATSL"); memcpy((xbsn_str+8),cmdOut,8); //convert to hex atox(xbsn,xbsn_str,XB_SN_LN); // flush buffer and collect garbage while(Radio->available() > 0) { Radio->read(); } delete cmdOut; }
/** * \brief Build a SansgridPeck from entries in the dictionary * \param[in] dict Populated Dictionary Structure * \param[in] size Number of entries in the Dictionary * \param[out] sg_serial Outputted SansgridSerial Structure * * Note that sg_serial must point to a valid chunk of memory. * \returns * The Device Identifier rdid is returned when found. \n * If the rdid is missing, return -1. */ static int8_t convertPeck(Dictionary dict[], int size, SansgridSerial *sg_serial) { // Get a peck datatype from the payload SansgridPeck sg_peck; uint32_t rdid; atox(&sg_peck.datatype, match(dict, size, "dt"), 1*sizeof(uint8_t)); atox(sg_peck.assigned_ip, match(dict, size, "server_ip"), IP_SIZE); atox(sg_peck.server_id, match(dict, size, "sid"), 16*sizeof(uint8_t)); atox(&sg_peck.recognition, match(dict, size,"recognition"),1*sizeof(uint8_t)); atox(sg_peck.manid, match(dict, size, "manid"), 4*sizeof(uint8_t)); atox(sg_peck.modnum, match(dict, size, "modnum"), 4*sizeof(uint8_t)); atox(sg_peck.serial_number, match(dict, size, "sn"), 8*sizeof(uint8_t)); memset(sg_peck.padding, 0x0, 15*sizeof(uint8_t)); routingTableGetRouterIP(routing_table, sg_peck.router_ip); rdid = atoi(match(dict, size, "rdid")); routingTableRDIDToIP(routing_table, rdid, sg_peck.assigned_ip); routingTableGetBroadcast(routing_table, sg_serial->ip_addr); memcpy(sg_serial->payload, &sg_peck, sizeof(SansgridPeck)); return translateRdid(dict, size); }
//----------------------------------------------------------------------------- // Command_Decode //----------------------------------------------------------------------------- // // Return Value : Error code // Parameters : Pointer to input string // // Parses the command string and calls the appropriate functions. // //----------------------------------------------------------------------------- uint8_t CommandDecode(char * instr) { char * cp; // character pointer const DEVICE_FAMILY *dfptr; // pointer to current device family const DERIVATIVE_ENTRY *deptr; // pointer to current derivative uint8_t command_number = 99; // number of command encoded in 'instr' uint8_t result = INVALID_COMMAND; printf("Command: %s\n", instr); command_number = 0xFF; if (instr[0] >= '0' && instr[0] <= '9') { // if first char is a digit, then interpret it as a command number command_number = atoi (instr); } { // interpret command as a string and find command number // or find command by command_number const COMMAND *ctptr = Commands; while (ctptr->name != NULL) { if (command_number != 0xFF) { if (ctptr->number == command_number) break; } else if (strncmp (instr, ctptr->name, ctptr->name_size) == 0) { // we've found the command, so record its number and exit command_number = ctptr->number; break; } ctptr++; } if (ctptr->name != NULL) { if (ctptr->need_discover && !FamilyFound) { result = DEVICE_UNDISCOVERED; command_number = 0xFE; // Unknown command } } else command_number = 0xFF; // Unknown command } // Now we have a command number, so act on it. switch (command_number) { case 0: // Device Autodetect { uint8_t deviceId = 0xFF; // initialize device and derivative uint8_t revisionId = 0xFF; uint8_t derivativeId = 0xFF; // id's to invalid selections printf("Device Autodetect\n"); Start_Stopwatch(); if (NO_ERROR == (result = C2_Halt ()) && NO_ERROR == (result = C2_Discover (&deviceId, &revisionId, &derivativeId)) ) { CommandsList = KnownFamilies[FamilyNumber].InitStrings; } Stop_Stopwatch(); printf("Device ID : %02X\n", deviceId); printf("Revision ID : %02X\n", revisionId); printf("Derivative ID : %02X\n", derivativeId); break; } case 1: // Print Menu { printf("? stub\n"); Start_Stopwatch(); Display_Menu(); Stop_Stopwatch(); result = NO_ERROR; break; } case 2: // Wait ms { uint16_t wait_time; cp = GetNextWord(instr); if (NO_ERROR == (result = CheckEmpty(cp))) { wait_time = atoi (cp); printf("Waiting %d ms\n", wait_time); Start_Stopwatch(); Wait_ms (wait_time); Stop_Stopwatch(); printf("Stopwatch_ms is %u\n", Stopwatch_ms); result = NO_ERROR; } break; } case 3: // Wait us { uint16_t wait_time; cp = GetNextWord(instr); if (NO_ERROR == (result = CheckEmpty(cp))) { wait_time = atoi (cp); printf("Waiting %d us\n", wait_time); Start_Stopwatch(); Wait_us (wait_time); Stop_Stopwatch(); printf("Stopwatch_us is %u\n", Stopwatch_us); result = NO_ERROR; } break; } case 4: // Start Stopwatch { printf("Start Stopwatch\n"); result = Start_Stopwatch(); break; } case 5: // Stop Stopwatch { printf("Stop Stopwatch\n"); result = Stop_Stopwatch(); printf("Stopwatch_ms is %u\n", Stopwatch_ms); printf("Stopwatch_us is %u\n", Stopwatch_us); break; } case 6: // Set Timeout ms { uint16_t wait_time; printf("Set Timeout ms:\n"); cp = GetNextWord(instr); if (NO_ERROR == (result = CheckEmpty(cp))) { wait_time = atoi (cp); printf("Timing out for %d ms\n", wait_time); Start_Stopwatch(); SetTimeout_ms (wait_time); SetTimeout_us (1); while (!IsDoneTimeout_ms()) ; Stop_Stopwatch(); printf("Stopwatch_ms is %u\n", Stopwatch_ms); result = NO_ERROR; } break; } case 7: // Set Timeout us { uint16_t wait_time; printf("Set Timeout us\n"); cp = GetNextWord(instr); if (NO_ERROR == (result = CheckEmpty(cp))) { wait_time = atoi(cp); printf("Timing out for %d us\n", wait_time); Start_Stopwatch(); SetTimeout_us(wait_time); while (!IsDoneTimeout_us()) ; Stop_Stopwatch(); printf("Stopwatch_us is %u\n", Stopwatch_us); result = NO_ERROR; } break; } case 8: // Pin init { printf("Pin Init\n"); result = Pin_Init(); break; } case 9: // C2 Reset { printf("C2 Reset\n"); result = C2_Reset(); break; } case 10: // C2 Write Address { uint16_t addr; cp = GetNextWord(instr); if (NO_ERROR == (result = CheckEmpty(cp))) { addr = atox (cp); printf("C2 Write Address: %02X\n", addr); Start_Stopwatch(); result = C2_WriteAR(addr); Stop_Stopwatch(); } break; } case 11: // C2 Read Address { Start_Stopwatch(); result = C2_ReadAR(); Stop_Stopwatch(); printf("C2 Read Address: %02X\n", (uint16_t) C2_AR); break; } case 12: // C2 Write Data { uint8_t data; cp = GetNextWord(instr); if (NO_ERROR == (result = CheckEmpty(cp))) { data = atox (cp); printf("C2 Write Data: %02X\n", (uint16_t) data); Start_Stopwatch (); result = C2_WriteDR (data, C2_WRITE_DR_TIMEOUT_US); Stop_Stopwatch (); } break; } case 13: // C2 Read Data { Start_Stopwatch (); result = C2_ReadDR (C2_READ_DR_TIMEOUT_US); Stop_Stopwatch (); printf("C2 Read Data: %02X\n", (uint16_t) C2_DR); break; } case 14: // C2 Reset and Halt { printf("C2 Reset and Halt\n"); result = C2_Halt (); break; } case 15: // C2 Get Device ID { uint8_t devId; printf("C2 Get Device ID\n"); Start_Stopwatch (); result = C2_GetDevID (&devId); Stop_Stopwatch (); printf("Device ID is %u, 0x%04X\n", devId, devId); break; } case 16: // C2 Get Revision ID { uint8_t revid; printf("C2 Get Revision ID\n"); Start_Stopwatch (); result = C2_GetRevID (&revid); Stop_Stopwatch (); printf("Revision ID is %u, 0x%04X\n", revid, revid); break; } case 17: // C2 Read SFR { uint8_t sfr_value, sfr_address; cp = GetNextWord(instr); if (NO_ERROR == (result = CheckEmpty(cp))) { sfr_address = atox (cp); Start_Stopwatch (); result = C2_ReadSFR (sfr_address, &sfr_value); Stop_Stopwatch (); printf("C2 Read SFR(%02X) %02X\n", (uint16_t) sfr_address, (uint16_t) sfr_value); } break; } case 18: // C2 Write SFR { uint8_t sfr_address, sfr_value; cp = GetNextWord(instr); if (NO_ERROR == (result = CheckEmpty(cp))) { sfr_address = atox (cp); cp = GetNextWord(cp); if (NO_ERROR == (result = CheckEmpty(cp))) { sfr_value = atox (cp); printf("C2 Write %02X to SFR(%02X)\n", (uint16_t) sfr_value, (uint16_t) sfr_address); Start_Stopwatch (); result = C2_WriteSFR (sfr_address, sfr_value); Stop_Stopwatch (); } } break; } case 19: // C2 Read Direct { uint8_t sfr_value, sfr_address; cp = GetNextWord(instr); if (NO_ERROR == (result = CheckEmpty(cp))) { sfr_address = atox (cp); Start_Stopwatch (); result = C2_ReadDirect (sfr_address, &sfr_value, C2_DIRECT); Stop_Stopwatch (); printf("C2 Read Direct(%02X) %02X\n", (uint16_t) sfr_address, (uint16_t) sfr_value); } break; } case 20: // C2 Write Direct <address> <value> { uint8_t sfr_address, sfr_value; cp = GetNextWord(instr); if (NO_ERROR == (result = CheckEmpty(cp))) { sfr_address = atox (cp); cp = GetNextWord(cp); if (NO_ERROR == (result = CheckEmpty(cp))) { sfr_value = atox (cp); printf("C2 Write %02x to Direct(%02X)\n", (uint16_t) sfr_value, (uint16_t) sfr_address); Start_Stopwatch (); result = C2_WriteDirect (sfr_address, sfr_value, C2_DIRECT); Stop_Stopwatch (); } } break; } case 21: // C2 Read Indirect { uint8_t sfr_value, sfr_address; cp = GetNextWord(instr); if (NO_ERROR == (result = CheckEmpty(cp))) { sfr_address = atox (cp); Start_Stopwatch (); result = C2_ReadDirect (sfr_address, &sfr_value, C2_INDIRECT); Stop_Stopwatch (); printf("C2 Read Indirect(%02X) %02X\n", (uint16_t) sfr_address, (uint16_t) sfr_value); } break; } case 22: // C2 Write Indirect { uint8_t sfr_address; uint8_t sfr_value; cp = GetNextWord(instr); if (NO_ERROR == (result = CheckEmpty(cp))) { sfr_address = atox (cp); cp = GetNextWord(cp); if (NO_ERROR == (result = CheckEmpty(cp))) { sfr_value = atox (cp); printf("C2 Write %02x to Indirect(%02X)\n", (uint16_t) sfr_value, (uint16_t) sfr_address); Start_Stopwatch (); result = C2_WriteDirect (sfr_address, sfr_value, C2_INDIRECT); Stop_Stopwatch (); } } break; } case 23: // C2 Discover { uint8_t j, deviceId, revisionId, derivativeId; printf("C2 Discover\n"); Start_Stopwatch (); result = C2_Discover (&deviceId, &revisionId, &derivativeId); Stop_Stopwatch (); if (result != NO_ERROR) break; dfptr = &(KnownFamilies[FamilyNumber]); deptr = &(KnownFamilies[FamilyNumber].DerivativeList[DerivativeNumber]); printf("Family Information:\n"); printf("Device ID: 0x%04X\n", dfptr->DEVICE_ID); printf("Family string: %s\n", dfptr->FAMILY_STRING); printf("Mem Type: %u\n", (uint16_t) dfptr->MEM_TYPE); printf("Page Size: %u\n", dfptr->PAGE_SIZE); printf("Has SFLE: %u\n", (uint16_t) dfptr->HAS_SFLE); printf("Security Type: %u\n", (uint16_t) dfptr->SECURITY_TYPE); printf("FPDAT address: 0x%02X\n", (uint16_t) dfptr->FPDAT); printf("Device ID: 0x%04X\n", dfptr->DEVICE_ID); printf("Init strings:\n"); for (j = 0; ; j++) { if (dfptr->InitStrings[j] == NULL) break; printf("%s\n", dfptr->InitStrings[j]); } printf("\n"); printf("Derivative Information\n"); printf("----------------------\n"); printf("Derivative ID : %02X\n", deptr->DERIVATIVE_ID); printf("Derivative String : %s\n", deptr->DERIVATIVE_STRING); printf("Features String : %s\n", deptr->FEATURES_STRING); printf("Package String : %s \n", deptr->PACKAGE_STRING); printf("Code Start Address : %05X\n", deptr->CODE_START); printf("Code Size : %05X\n", deptr->CODE_SIZE); printf("Write Lock Byte Addr : %05X\n", deptr->WRITELOCKBYTEADDR); printf("Read Lock Byte Addr : %05X\n", deptr->READLOCKBYTEADDR); printf("Code 2 Start Address : %05X\n", deptr->CODE2_START); printf("Code 2 Size : %05X\n", deptr->CODE2_SIZE); printf("\n"); break; } case 24: // Run Init String { result = NO_ERROR; printf("Execute Device Init String:\n"); if (FamilyFound == true) CommandsList = KnownFamilies[FamilyNumber].InitStrings; else printf("Device not connected.\n"); break; } case 25: // C2 Flash Read <start addr> <length> { uint32_t addr; uint16_t length; printf("C2 Flash Read\n"); cp = GetNextWord(instr); if (NO_ERROR == (result = CheckEmpty(cp))) { addr = atolx (cp); cp = GetNextWord(cp); if (NO_ERROR == (result = CheckEmpty(cp))) { length = atoi (cp); if (length > sizeof (BinDest)) length = sizeof (BinDest); printf( "Reading %u bytes starting at address 0x%05lX\n", length, (unsigned long)addr ); Start_Stopwatch (); result = C2_FLASH_Read (BinDest, addr, length); Stop_Stopwatch (); BIN2HEXSTR (HexDest, BinDest, length); printf("Memory contents are %s\n", HexDest); } } break; } case 26: // C2 OTP Read <start addr> <length> { uint32_t addr; uint16_t length; printf("C2 OTP Read\n"); cp = GetNextWord(instr); if (NO_ERROR == (result = CheckEmpty(cp))) { addr = atolx (cp); cp = GetNextWord(cp); if (NO_ERROR == (result = CheckEmpty(cp))) { length = atoi (cp); if (length > sizeof (BinDest)) length = sizeof (BinDest); printf("Reading %u bytes starting at address 0x%05lX\n", length, (unsigned long)addr ); Start_Stopwatch (); result = C2_OTP_Read (BinDest, addr, length); Stop_Stopwatch (); BIN2HEXSTR (HexDest, BinDest, length); printf("Memory contents are %s\n", HexDest); } } break; } case 27: // C2 Flash Write <start addr> <hex string> { uint32_t addr; uint8_t length; printf("C2 Flash Write\n"); cp = GetNextWord(instr); if (NO_ERROR == (result = CheckEmpty(cp))) { addr = atolx (cp); cp = GetNextWord(cp); if (NO_ERROR == (result = CheckEmpty(cp))) { // warning! 'dest' could be overtaken by a long string if (NO_ERROR == (result = HEXSTR2BIN (BinDest, cp, &length, sizeof(BinDest)))) { printf("Writing %u bytes starting at address 0x%05X\n", length, addr); // printf("Writing the following string: %s\n", cp); Start_Stopwatch (); result = C2_FLASH_Write (addr, BinDest, length); Stop_Stopwatch (); } } } break; } case 28: // C2 OTP Write <start addr> <hex string> { uint32_t addr; uint8_t length; printf("C2 OTP Write\n"); cp = GetNextWord(instr); if (NO_ERROR == (result = CheckEmpty(cp))) { addr = atolx (cp); cp = GetNextWord(cp); if (NO_ERROR == (result = CheckEmpty(cp))) { if (NO_ERROR != (result = HEXSTR2BIN (BinDest, cp, &length, sizeof(BinDest)))) { printf("Hex string too long"); break; } printf( "Writing %u bytes starting at address 0x%05lX\n", (uint16_t) length, (unsigned long)addr ); printf("Writing the following string: %s\n", cp); Start_Stopwatch (); result = C2_OTP_Write (addr, BinDest, length); Stop_Stopwatch (); } } break; } case 29: // C2 Page Erase <address in page to erase> { uint32_t addr; printf("C2 Flash Page Erase\n"); cp = GetNextWord(instr); if (NO_ERROR == (result = CheckEmpty(cp))) { addr = atolx (cp); printf("Erasing page containing address 0x%05X\n", addr); Start_Stopwatch (); result = C2_FLASH_PageErase (addr); Stop_Stopwatch (); } break; } case 30: // C2 Device Erase { printf("C2 Flash Device Erase\n"); printf("Erasing device...\n"); Start_Stopwatch (); result = C2_FLASH_DeviceErase (); Stop_Stopwatch (); break; } case 31: // C2 Flash Blank Check { uint32_t addr; uint32_t length; printf("C2 Flash Blank Check\n"); addr = KnownFamilies[FamilyNumber].DerivativeList[DerivativeNumber].CODE_START; length = KnownFamilies[FamilyNumber].DerivativeList[DerivativeNumber].CODE_SIZE; printf("Checking starting at address 0x%05X for 0x%05X bytes: ", addr, length); Start_Stopwatch (); result = C2_FLASH_BlankCheck (addr, length); Stop_Stopwatch (); printf((result == DEVICE_IS_BLANK) ? "OK\n" : "Fail\n"); break; } case 32: // C2 OTP Blank Check { uint32_t addr; uint32_t length; printf("C2 OTP Blank Check\n"); addr = KnownFamilies[FamilyNumber].DerivativeList[DerivativeNumber].CODE_START; length = KnownFamilies[FamilyNumber].DerivativeList[DerivativeNumber].CODE_SIZE; printf("Checking starting at address 0x%05X for 0x%05X bytes: ", addr, length); Start_Stopwatch (); result = C2_OTP_BlankCheck (addr, length); Stop_Stopwatch (); printf((result == NO_ERROR) ? "OK\n" : "Fail\n"); break; } case 33: // C2 Get Lock Byte value { printf("C2 Get Lock Byte\n"); break; } case 34: // Write Target to HEX { printf("Write Target to HEX:\n"); Start_Stopwatch (); result = OP_Write_TARGET2HEX(); Stop_Stopwatch (); break; } case 36: // Write HEX to Target { HEX_RECORD hex; printf("Write HEX to Target:\n"); cp = GetNextWord(instr); if (NO_ERROR == (result = CheckEmpty(cp))) { hex.Buf = BinDest; result = HEX_Decode(&hex, cp, sizeof(BinDest)); if (result == NO_ERROR && hex.RECLEN != 0) { printf("Writing %u bytes starting at address 0x%05X\n", hex.RECLEN, hex.OFFSET.U16); Start_Stopwatch (); result = C2_FLASH_Write (hex.OFFSET.U16, hex.Buf, hex.RECLEN); Stop_Stopwatch (); } else if (result == EOF_HEX_RECORD) result = NO_ERROR; } break; } case 35: // Read SFRs and directs { uint8_t row; uint8_t col; uint8_t value; Start_Stopwatch (); for (row = 0xF8; row != 0x00; row = row - 8) { for (col = 0; col != 0x08; col++) { if (NO_ERROR != (result = C2_ReadDirect ((row+col), &value, C2_DIRECT))) break; if (col == 0) printf("\n0X%02X: %02X", (uint16_t) (row), (uint16_t) value); else printf(" %02X", (uint16_t) value); } } printf("\n\n"); Stop_Stopwatch (); break; } case 0xFE: break; default: { result = INVALID_COMMAND; } } printf("Result: %02X %s\n", result, GetErrorName(result)); return result; }
int main(void) { char rule[20]; char bin[20]; int error = 1; //set to 0 if an unrecoverable error occurs global.state=0; global.table_entry = 0; global.rule.v_end=0x0; printf("Name of rule file: "); scanf("%s", rule); if(NULL==(global.rulefile=fopen(rule,"r"))) { printf("Error 1: Couldn't open Rule file\n"); return -1; } printf("Name of output file for Level 1 table: "); scanf("%s", bin); if(NULL==(global.outfile=fopen(bin,"wb"))) { fclose(global.outfile); printf("Error 2: Couldn't open Output file\n"); return -1; } printf("Name of page table entry output file: "); scanf("%s", bin); if(NULL==(global.entry_outfile=fopen(bin,"wb"))) { fclose(global.entry_outfile); printf("Error 2: Couldn't open Output file\n"); return -1; } printf("Do you have a Level 2 table? (y/n) "); scanf("%s", bin); /* Level 2 table was defined */ if(bin[0] == 'y') { printf("Name of output file for Level 2 table: "); scanf("%s", bin); if(NULL==(global.l2_outfile=fopen(bin,"wb"))) { fclose(global.l2_outfile); printf("Error 2: Couldn't open Output file\n"); return -1; } printf("Base address of Level 2 table: "); scanf("%s", bin); global.base_address_l2 = atox(bin); } /* Level 2 table was not defined */ else { /* setting Level 2 table base to invalid value to check whether Level 2 was specified when it should not have been */ global.base_address_l2 = 0xFFFFFFFF; } while( ParseRule(&error) ) { if(WriteTableEntries(&error)==-1) { fclose( global.outfile ); fclose( global.rulefile ); fclose( global.entry_outfile ); fclose( global.l2_outfile ); exit(-1); } //check for terminal error if(!error) { fclose( global.outfile ); fclose( global.rulefile ); fclose( global.entry_outfile ); fclose( global.l2_outfile ); printf("Error 3: MMU table could not be generated\n"); return 0 ; } global.rule.def = 0; //clear default flag } printf("MMU table generation complete\n"); fclose( global.outfile ); fclose( global.rulefile ); fclose( global.entry_outfile ); fclose( global.l2_outfile ); return 0 ; }
/** * \brief Convert an intrarouter null-terminated key-value string * into formatted data * * \param[in] payload Null-terminated key-value string * \param[out] sg_serial Sansgrid Serial Formatted String * \returns * On success, return 0 \n * On failure, return -1 */ int8_t sgServerToRouterConvert(char *payload, SansgridSerial *sg_serial) { // Take a payload, return the translated serial packet, // return the device identifier (rdid) uint8_t datatype = ~0x0; enum SansgridDeviceStatusEnum dev_datatype; Dictionary dict[30]; int32_t size = 0; int8_t exit_code = 0; uint32_t rdid_32; char *rdid_str; char *saved = NULL, *key = NULL, *value = NULL; sg_serial->control = SG_SERIAL_CTRL_VALID_DATA; memset(sg_serial->ip_addr, 0x0, sizeof(sg_serial->ip_addr)); syslog(LOG_DEBUG, "processing packet %s", payload); do { if (extract_keyvalue(payload, &key, &value, &saved) == 1) { dict[size].key = key+sizeof(DELIM_KEY)-2; dict[size].value = (value == NULL ? NULL : value+sizeof(DELIM_VAL)-2); size++; } else break; } while (1); dict[size].key = NULL; dict[size].value = NULL; // Find payload type atox(&datatype, match(dict, size, "dt"), 1*sizeof(uint8_t)); //atox(rdid, match(dict, size, "rdid"), 4*sizeof(uint8_t)); rdid_str = match(dict, size, "rdid"); if (rdid_str == NULL) { syslog(LOG_WARNING, "Couldn't find rdid in payload %s", payload); rdid_32 = 0; } else { rdid_32 = atoi(rdid_str); } if (datatype == ~0x0) { return -1; } if (!rdid_32) { // broadcast //routingTableGetBroadcast(routing_table, sg_serial->ip_addr); } else { routingTableRDIDToIP(routing_table, rdid_32, sg_serial->ip_addr); } dev_datatype = sgPayloadGetType(datatype); switch(dev_datatype) { case SG_DEVSTATUS_EYEBALLING: exit_code = convertEyeball(dict, size, sg_serial); break; case SG_DEVSTATUS_PECKING: exit_code = convertPeck(dict, size, sg_serial); break; case SG_DEVSTATUS_SINGING: exit_code = convertSing(dict, size, sg_serial); break; case SG_DEVSTATUS_MOCKING: exit_code = convertMock(dict, size, sg_serial); break; case SG_DEVSTATUS_PEACOCKING: exit_code = convertPeacock(dict, size, sg_serial); break; case SG_DEVSTATUS_NESTING: exit_code = convertNest(dict, size, sg_serial); break; case SG_DEVSTATUS_SQUAWKING: exit_code = convertSquawk(dict, size, sg_serial); break; case SG_DEVSTATUS_CHIRPING: exit_code = convertChirp(dict, size, sg_serial); break; case SG_DEVSTATUS_HEARTBEAT: exit_code = convertIRStatus(dict, size, sg_serial); break; default: printf("Type not found: %u\n", dev_datatype); exit_code = -1; break; } //printf("Exiting with %i\n", exit_code); return exit_code; }
int ParseRule (int* error) { char buffer[20]; int i; /* fetch next word from rule file */ while(GetWord(buffer)) { /* set the appropriate entry bit depending on the word fetched from the rule file */ switch( GetWordIndex(buffer) ) { /* grabs the base address of the mmu table */ case BASE_ADDRESS: GetWord(buffer); global.base_address = atox(buffer); //converts the address into a decimal value printf("Base Address = 0x%x \n", global.base_address); fprintf(global.entry_outfile, "Base Address = 0x%x \n", global.base_address); break; /* mmu table using v6 format table entries */ case VSIX: global.rule.v6 = 1; printf("Format = v6 \n"); fprintf(global.entry_outfile, "Format = v6 \n"); break; /* mmu table using v7 format table entries */ case VSEVEN: global.rule.v7 = 1; printf("Format = v7 \n"); fprintf(global.entry_outfile, "Format = v7 \n"); break; /* grabs the Level number (1 or 2) */ case LEVEL: GetWord(buffer); global.state = atoi(buffer); //converts the level number to a decimal value /* checks to see if the Level 1 table is 4096 entries */ if(global.table_entry > 0x1000) printf("Warning 1: The Level 1 page table is %d entries, instead of 4096\n",global.table_entry); printf("Level = %d \n",global.state); fprintf(global.entry_outfile, "Level = %d \n", global.state); break; /* defines default table entry for missing memory regions */ case DEFAULT: global.rule.def = 1; /* defined default for L1 page table */ if( global.state == 1 ) global.rule.def1 = 1; /* defined default for L2 page table */ else global.rule.def2 = 1; break; /* grabs the start address of the virtual memory region */ case VIRTUAL: GetWord(buffer); global.rule.v_base = atox(buffer); //converts the address into a decimal value printf("Virtual Address = 0x%x ",global.rule.v_base); fprintf(global.entry_outfile, "--- Virtual Address = 0x%x ",global.rule.v_base); break; /* separates the starting virtual address from the ending virtual address */ case TO: global.rule.v_end_saved = global.rule.v_end; //save end address of last section GetWord(buffer); //grabs the end address of the virtual memory region global.rule.v_end = atox(buffer); //converts the address into a decimal value printf("to 0x%x ",global.rule.v_end); fprintf(global.entry_outfile, "to 0x%x ",global.rule.v_end); break; /* grabs the start address of the physical memory region */ case PHYSICAL: GetWord(buffer); global.rule.p_base = atox(buffer); //converts the address into a decimal value printf("Physical Address = 0x%x \n",global.rule.p_base); fprintf(global.entry_outfile, "Physical Address = 0x%x ---\n",global.rule.p_base); break; /* the memory region is divided into 1MB fault sections */ case FAULT: /* setting default table entry for missing memory regions */ if( global.rule.def ) global.rule.d_entrytype = 'F'; else global.rule.entrytype = 'F'; /* have hit the end of the rule entry */ return 1; /* the memory region is divided into pages (4KB or 64KB) */ case PAGES: /* setting default table entry for missing memory regions */ if( global.rule.def ) global.rule.d_entrytype = 'P'; else global.rule.entrytype = 'P'; /* have hit the end of the rule entry */ return 1; /* the memory region is divided into sections (1MB) */ case SECTION: /* setting default table entry for missing memory regions */ if( global.rule.def ) global.rule.d_entrytype = 'S'; else global.rule.entrytype = 'S'; /* have hit the end of the rule entry */ return 1; /* the memory region is divided into supersections (16MB) */ case SUPERSECTION: /* setting default table entry for missing memory regions */ if( global.rule.def ) global.rule.d_entrytype = 'H'; else global.rule.entrytype = 'H'; /* have hit the end of the rule entry */ return 1; /* sets APX bit to 0 */ case READWRITE: /* setting default table entry for missing memory regions */ if( global.rule.def ) global.rule.d_ro = 0; else global.rule.ro = 0; break; /* sets APX bit to 1 */ case READONLY: /* setting default table entry for missing memory regions */ if( global.rule.def ) global.rule.d_ro = 1; else global.rule.ro = 1; break; /* sets AP bits to 0 */ case NO_ACCESS: /* a v7 page table entry cannot have attributes of READONLY and NO_ACCESS */ if( global.rule.v7 == 1 && global.rule.ro == 1 ) { printf("Error 4: READONLY and NO_ACCESS are not attributes for v7 page tables \n"); *error = 0; //generated binary file will be incorrect return 0; } /* setting default table entry for missing memory regions */ if( global.rule.def ) global.rule.d_access = 0 ; else global.rule.access = 0 ; break; /* sets AP bits to 1 */ case SVC_READWRITE: /* setting default table entry for missing memory regions */ if( global.rule.def ) global.rule.d_access = 1 ; else global.rule.access = 1 ; break; /* sets AP bits to 2 */ case NO_USR_WRITE: /* setting default table entry for missing memory regions */ if( global.rule.def ) global.rule.d_access = 2 ; else global.rule.access = 2 ; break; /* sets AP bits to 3 */ case FULL_ACCESS: /* setting default table entry for missing memory regions */ if( global.rule.def ) global.rule.d_access = 3 ; else global.rule.access = 3 ; break; /* sets S bit to 1 */ case SHARED: /* setting default table entry for missing memory regions */ if( global.rule.def ) global.rule.d_shared = 1; else global.rule.shared = 1; break; /* sets nG bit to 1 */ case NOT_GLOBAL: /* setting default table entry for missing memory regions */ if( global.rule.def ) global.rule.d_nglobal = 1; else global.rule.nglobal = 1; break; /* sets XN bit to 1 */ case EXECUTE_NEVER: /* setting default table entry for missing memory regions */ if( global.rule.def ) global.rule.d_execn = 1; else global.rule.execn = 1; break; /* sets NS bit to 1 */ case NOT_SECURE: /* setting default table entry for missing memory regions */ if( global.rule.def ) { /* only set the NS bit if using v6/v7 page table format*/ if ( global.rule.v6 == 1 || global.rule.v7 == 1) { global.rule.d_n_s = 1; } } else { /* only set the NS bit if using v6/v7 page table format*/ if ( global.rule.v6 == 1 || global.rule.v7 == 1) { global.rule.n_s = 1; } } break; /* sets P bit to 1 */ case EEC: /* setting default table entry for missing memory regions */ if( global.rule.def ) global.rule.d_p = 1; else global.rule.p = 1; break; /* sets Domain Selector to 15 */ case DOMAIN_FIFTEEN: /* setting default table entry for missing memory regions */ if( global.rule.def ) global.rule.d_domain = 15; else global.rule.domain = 15; break; /* sets Domain Selector to 14 */ case DOMAIN_FOURTEEN: /* setting default table entry for missing memory regions */ if( global.rule.def ) global.rule.d_domain = 14; else global.rule.domain = 14; break; /* sets Domain Selector to 13 */ case DOMAIN_THIRTEEN: /* setting default table entry for missing memory regions */ if( global.rule.def ) global.rule.d_domain = 13; else global.rule.domain = 13; break; /* sets Domain Selector to 12 */ case DOMAIN_TWELVE: /* setting default table entry for missing memory regions */ if( global.rule.def ) global.rule.d_domain = 12; else global.rule.domain = 12; break; /* sets Domain Selector to 11 */ case DOMAIN_ELEVEN: /* setting default table entry for missing memory regions */ if( global.rule.def ) global.rule.d_domain = 11; else global.rule.domain = 11; break; /* sets Domain Selector to 10 */ case DOMAIN_TEN: /* setting default table entry for missing memory regions */ if( global.rule.def ) global.rule.d_domain = 10; else global.rule.domain = 10; break; /* sets Domain Selector to 9 */ case DOMAIN_NINE: /* setting default table entry for missing memory regions */ if( global.rule.def ) global.rule.d_domain = 9; else global.rule.domain = 9; break; /* sets Domain Selector to 8 */ case DOMAIN_EIGHT: /* setting default table entry for missing memory regions */ if( global.rule.def ) global.rule.d_domain = 8; else global.rule.domain = 8; break; /* sets Domain Selector to 7 */ case DOMAIN_SEVEN: /* setting default table entry for missing memory regions */ if( global.rule.def ) global.rule.d_domain = 7; else global.rule.domain = 7; break; /* sets Domain Selector to 6 */ case DOMAIN_SIX: /* setting default table entry for missing memory regions */ if( global.rule.def ) global.rule.d_domain = 6; else global.rule.domain = 6; break; /* sets Domain Selector to 5 */ case DOMAIN_FIVE: /* setting default table entry for missing memory regions */ if( global.rule.def ) global.rule.d_domain = 5; else global.rule.domain = 5; break; /* sets Domain Selector to 4 */ case DOMAIN_FOUR: /* setting default table entry for missing memory regions */ if( global.rule.def ) global.rule.d_domain = 4; else global.rule.domain = 4; break; /* sets Domain Selector to 3 */ case DOMAIN_THREE: /* setting default table entry for missing memory regions */ if( global.rule.def ) global.rule.d_domain = 3; else global.rule.domain = 3; break; /* sets Domain Selector to 2 */ case DOMAIN_TWO: /* setting default table entry for missing memory regions */ if( global.rule.def ) global.rule.d_domain = 2; else global.rule.domain = 2; break; /* sets Domain Selector to 1 */ case DOMAIN_ONE: /* setting default table entry for missing memory regions */ if( global.rule.def ) global.rule.d_domain = 1; else global.rule.domain = 1; break; /* sets Domain Selector to 0 */ case DOMAIN_ZERO: /* setting default table entry for missing memory regions */ if( global.rule.def ) global.rule.d_domain = 0; else global.rule.domain = 0; break; /* sets C bit to 1 */ case CACHEABLE: /* setting default table entry for missing memory regions */ if( global.rule.def ) global.rule.d_cb |= 2; else global.rule.cb |= 2; break; /* sets B bit to 1 */ case BUFFERABLE: /* setting default table entry for missing memory regions */ if( global.rule.def ) global.rule.d_cb |= 1; else global.rule.cb |= 1; break; /* sets TEX bits to 4 */ case OUTER: global.rule.outer = 1; //outer cache attributes have been set /* setting outer cache as non-cacheable, non-bufferable */ if( global.rule.def ) global.rule.d_tex = 4; else global.rule.tex = 4; break; /* sets TEX bits to 5 */ case OUTER_B: global.rule.outer = 1; //outer cache attributes have been set /* setting outer cache as non-cacheable, bufferable */ if( global.rule.def ) global.rule.d_tex = 5; else global.rule.tex = 5; break; /* sets TEX bits to 6 */ case OUTER_C: global.rule.outer = 1; //outer cache attributes have been set /* setting outer cache as cacheable, non-bufferable */ if( global.rule.def ) global.rule.d_tex = 6; else global.rule.tex = 6; break; /* sets TEX bits to 7 */ case OUTER_CB: global.rule.outer = 1; //outer cache attributes have been set /* setting outer cache as cacheable, bufferable */ if( global.rule.def ) global.rule.d_tex = 7; else global.rule.tex = 7; break; /* marks memory as Normal by setting the TEX bits accordingly */ case NORMAL: /* already specified TEX because outer cacheable attributes have been declared */ if(global.rule.outer) { printf("Warning 2: Ignoring Normal memory type, TEX bits have already been set.\n"); break; } /* cacheable Normal memory */ if((global.rule.cb & 2) == 2) { /* setting default table entry for missing memory regions */ if( global.rule.def ) global.rule.d_tex = 0; //TEX bits = 0 else global.rule.tex = 0; //TEX bits = 0 } /* not cacheable Normal memory */ else { /* not bufferable, not cacheable Normal memory */ if((global.rule.cb & 1) == 0) { /* setting default table entry for missing memory regions */ if( global.rule.def ) global.rule.d_tex = 1; //TEX bits = 1 else global.rule.tex = 1; //TEX bits = 1 } /* bufferable, not cacheable Normal memory - not allowed */ else { printf("Error 5: Bufferable, not cacheable Normal memory is not allowed.\n"); *error = 0; //generated binary file will be incorrect return 0; } } break; /* marks memory as Device by setting the TEX bits accordingly */ case DEVICE: /* already specified TEX because outer cacheable attributes have been declared */ if(global.rule.outer) { printf("Warning 3: Ignoring Device memory type, TEX bits have already been set.\n"); break; } /* cacheable Device memory - not allowed */ if ((global.rule.cb & 2) == 2) { printf("Error 6: Device memory cannot be cacheable.\n"); *error = 0; //generated binary file will be incorrect return 0; } /* not cacheable, bufferable Device memory */ else if((global.rule.cb & 1) == 1) { /* setting default table entry for missing memory regions */ if( global.rule.def ) global.rule.d_tex = 0; //TEX bits = 0 else global.rule.tex = 0; //TEX bits = 0 } /* not cacheable, not bufferable Device memory */ else { /* setting default table entry for missing memory regions */ if( global.rule.def ) global.rule.d_tex = 2; //TEX bits = 2 else global.rule.tex = 2; //TEX bits = 2 } break; /* marks memory as Strongly Ordered by setting the TEX bits accordingly */ case STRONGLY_ORDERED: /* already specified TEX because outer cacheable attributes have been declared */ if(global.rule.outer) { printf("Warning 4: Ignoring Strongly Ordered memory type, TEX bits have already been set.\n"); break; } /* not cacheable, not bufferable Strongly Ordered memory*/ if(global.rule.cb == 0) { /* setting default table entry for missing memory regions */ if( global.rule.def ) global.rule.d_tex = 0; //TEX bits = 0 else global.rule.tex = 0; //TEX bits = 0 } /* cacheable or bufferable Strongly Ordered memory - not allowed */ else { printf("Error 7: Strongly Ordered memory cannot be cacheable or bufferable.\n"); *error = 0; //generated binary file will be incorrect return 0; } break; /* the memory region is divided into largepages (64KB) */ case LARGEPAGES: /* setting default table entry for missing memory regions */ if( global.rule.def ) global.rule.d_entrytype = 'L'; else global.rule.entrytype = 'L'; /* have hit the end of the rule entry */ return 1; /* the memory region is divided into smallpages (4KB) */ case SMALLPAGES: /* setting default table entry for missing memory regions */ if( global.rule.def ) global.rule.d_entrytype = 'S'; else global.rule.entrytype = 'S'; /* have hit the end of the rule entry */ return 1; /* the memory region is divided into extended_smallpages (4KB) */ case EXTENDED_SMALLPAGES: /* setting default table entry for missing memory regions */ if( global.rule.def ) global.rule.d_entrytype = 'E'; else global.rule.entrytype = 'E'; /* have hit the end of the rule entry */ return 1; /* modifies a particular page table entry after it have been generated */ case POSTPROCESS: GetWord(buffer); //skip ENTRY GetWord(buffer); //get address fseek( global.outfile, atox(buffer) ,SEEK_SET ); GetWord(buffer); //skip EQUALS GetWord(buffer); //get value i = atox(buffer); fwrite( &i, 4, 1, global.outfile ); break ; /* Have hit an unknown entry in the rule file */ case UNRECOGNISED: printf("Error 8: Unrecognized word '%s'\n",buffer); *error = 0; //generated binary file will be incorrect return 0; } } // must have hit end of rule file without finding the end of a rule return 0; }
// returns number of items successfully read int scanf(const char *format, ...) { va_list val; int scanned = 0; int64_t read_chars = 0; char c[MAX_STRING] = {0}; char *pStr = NULL; char *char_addr = NULL; unsigned int *hex_addr = NULL; int *int_addr = NULL; va_start(val, format); while(*format) { if (*format == '%') { format++; switch (*format) { case 'c': char_addr = va_arg(val, char *); read_chars = read_variable(0, c, 2); if (read_chars <= 0) return read_chars; *char_addr = c[0]; scanned++; break; case 's': pStr = va_arg(val, char *); read_chars = read_variable(0, c, MAX_STRING); if (read_chars <= 0) return read_chars; strncpy(pStr, c, MAX_STRING); scanned++; break; case 'd': int_addr = va_arg(val, int *); read_chars = read_variable(0, c, MAX_STRING); if (read_chars <= 0) return read_chars; if (atoi(int_addr, c) < 0) return -1; scanned++; break; case 'x': case 'X': hex_addr = va_arg(val, unsigned int *); read_chars = read_variable(0, c, MAX_STRING); if (read_chars <= 0) return read_chars; if (atox(hex_addr, c) < 0) return -1; scanned++; break; default: // Not supported... Ignore break; } } ++format; }