Init basesystem source codes.
[staging/basesystem.git] / video_in_hal / vehicleservice / positioning_base_library / library / src / _pbCommon.cpp
1 /*
2  * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /*
18 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
19  File name      : _pbCommon.cpp
20  System name    : 05 Integration Platform
21  Subsystem name : System wide functions
22  Title          : System API _CWORD64_ configuration processes
23 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
24 */
25 #include <string.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <vehicle_service/positioning_base_library.h>
30
31 #include "_pbCommon.h"
32 #include "WPF_STD_private.h"
33 #include "sysup_addr.h"
34 #include "tchar.h"
35 #include "sysup.h"
36
37 /*
38  Constants and data type definitions
39 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
40 #define BLOCK_SIZE          (1*1024*1024)
41 #define WDTC_REG_ADDR       (0x13500000 | ADDR_P2)  /* WDT clear register address */
42 #define SUM_ZERO            0
43 #define ALLOC_SIZE          0x00200000
44 #define EXIST_CLEAR      0x434c4f4b        /* Flag to indicate cleared Non-OS management area */
45
46 #define PRCM_BASE          0x48180000
47 #define PRM_RSTCTRL          0xA0
48
49 #define RST_GLOBAL_COLD_SW      (0x01 << 1)
50 #define RST_GLOBAL_WARM_SW      (0x01 << 0)
51
52 /*
53  Internal function prototype declarations
54 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
55 u_int32  RomCheck(u_int32 *start_addr, u_int32 check_size);
56 u_int32  RomCheckEx(u_int32 *start_addr, u_int32 check_size);
57 uint64_t AtoxLong(char* str, unsigned int ascii_size);
58 void XtoaLong(uint64_t x, char* str);
59 #ifdef PBCOM_CONFIGREAD
60 static RET_API SysGetConfigSize(FILE*, u_int32*);
61 static RET_API SysReadConfigInfo(FILE*, TCHAR*, TCHAR*, u_int32);
62 static int SysUniToInt(TCHAR*, u_int32);
63 #endif   /* PBCOM_CONFIGREAD */
64
65 /*
66  External function prototype declarations
67 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
68
69 /*
70  Global Variable Definitions
71 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
72 #ifdef PSL_USED
73 extern int        g_n_api_set_id;  /* ID variable for PSL registration */
74 #endif   // PSL_USED
75
76 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
77  * MODULE    : _CWORD64_NotOSAreaInit
78  * ABSTRACT  : Initialization non-OS managed areas of _CWORD64_ 
79  * NOTE      : This function initializes the non-OS control areas of _CWORD64_.
80  * ARGUMENT  : None
81  * RETURN    : RET_API defined
82  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
83 RET_API
84 _CWORD64_NotOSAreaInit(void) {  // LCOV_EXCL_START 8:dead code
85     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
86 #ifndef CEPC_EM    /* If not emulated in CEPC */
87     RET_API     ret;
88     char        *cp_addr = NULL;
89     u_int32     size;
90     u_int32     *p_clear_flag;       /* Pointer for storing non-OS management area clear flag */
91     LPVOID      notos_topaddr;
92
93     /* Link of shared memory area to store the non-OS management area clear flag */
94     ret = _pb_LinkShareData(const_cast<char*>("NOTOS_DATA"), reinterpret_cast<void**>(&cp_addr), &size);
95     if (ret != RET_NORMAL) {
96         /* Create a shared memory area to store the non-OS management area clear flag */
97         Memset64pSync(cp_addr, 0, sizeof(p_clear_flag));    /* NULL initialization process of acquisition area */
98     }
99
100     p_clear_flag = reinterpret_cast<u_int32*>(cp_addr);
101
102     /* Non-OS management area clear processing */
103     /* Because an exception occurs when garbage data remains in the message pool at startup,
104         Clear the entire non-OS management area as 0 Only for the first time after startup. */
105     if (*p_clear_flag != EXIST_CLEAR) {
106         /* Execute mapping to non-OS management area */
107         ret = PbAccessPhysicalMem(TOP_ADDR_NOTOS,      /* Start address non-OS management area for of the _CWORD64_ */
108                                       &notos_topaddr,      /* Start address of the mapping        */
109                                       NOTOS_SISE,        /* Area-size              */
110                                       0);
111
112         /* failed mapping */
113         if (ret != RET_NORMAL) {
114             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, 
115                                    "_CWORD64_msg.lib:%s:LINE %d\r\n PbAccessPhysicalMem ERROR In " \
116                                    "_CWORD64_NotOSAreaInit ... ret[%d] \r\n",
117                            LTEXT(__FILE__), __LINE__, ret);
118             _pb_Exit();
119         }
120
121         /* Non-OS management area clear processing */
122         Memset64pSync(notos_topaddr, 0, NOTOS_SISE);
123
124         /* Shared area release processing */
125         ret = PbFreePhysicalMem(TOP_ADDR_NOTOS,      /* Start address of the _CWORD64_ non-OS management area    */
126                                     notos_topaddr,      /* Start address of the mapped area    */
127                                     NOTOS_SISE);      /* Size                */
128
129         if (ret != RET_NORMAL) {
130             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, 
131                                    "_CWORD64_msg.lib:%s:LINE %d\r\n PbFreePhysicalMem ERROR " \
132                                    "In_CWORD64_NotOSAreaInit ... ret[%d] \r\n",
133                            LTEXT(__FILE__), __LINE__, ret);
134             _pb_Exit();
135         }
136
137         *p_clear_flag = EXIST_CLEAR;         /* Set non-OS management area clear flag */
138     }
139 #endif
140
141     return(RET_NORMAL);
142 }
143 // LCOV_EXCL_STOP
144
145 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
146  * MODULE    : _CWORD64_APIInit
147  * ABSTRACT  : System initialization of _CWORD64_API
148  * NOTE      : This function is called once from the initialization process
149  *           : at system startup to perform the _CWORD64_API initialization process.
150  * ARGUMENT  : u_int32 (*sighand)()     Completion of signal handling within APIs if NULL
151  *           :                          Otherwise calls this first when a signal is raised
152  * RETURN    : RET_API defined
153  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
154 RET_API
155 _CWORD64_APIInit(u_int32 (*sighand)()) {    // NOLINT(readability/nolint) API  // LCOV_EXCL_START 8:dead code
156     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
157     RET_API    RetSts = RET_ERROR;
158
159     if (sighand == NULL) {
160         /* Parameter is set to an error other than NULL */
161         RetSts = _pb_Setup_CWORD64_API(NULL);    /* Execute initialization processing for normal processing */
162     }
163
164     return(RetSts);
165 }
166 // LCOV_EXCL_STOP
167
168 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
169  * MODULE    : _pb_GetPrgVer
170  * ABSTRACT  : Get the program version
171  * NOTE      : Get the program version of the mask ROM (BootLoader) and flash 
172  *           : ROM (NK-image) into specified buffers.
173  *           : In hardware-independent CEPC emulation environment, return the following version,
174  *           : otherwise always return RET_NORMAL as the return value.
175  *           : mask_ver=0x0102 0304 0506 0708
176  *           : flash_ver1=0x0102 0304 0506 0708
177  *           : flash_ver2=0x0102 0304 0506 0708
178  *           : flash_ver3=0x0102 0304 0506 0708
179  * ARGUMENT  : T_SYS_PRGVER     *pbuf   Destination buffer address
180  * RETURN    : RET_API defined
181  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
182 RET_API
183 _pb_GetPrgVer(T_SYS_PRGVER *pbuf) {  // LCOV_EXCL_START 8:dead code
184     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
185     return(RET_NORMAL);
186 }
187 // LCOV_EXCL_STOP
188
189 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
190  * MODULE    : _pb_UpManage
191  * ABSTRACT  : Boot management process
192  * NOTE      : Set the startup identifier in the SDRAM startup identification table (SDRAM) 
193  *           : or TinyFugue area (FLASH) at the next startup.
194  *           : Which one to set depends on the table type of the argument.
195  *           : Soft reset is performed when reset is instructed by the parameter.
196  *           : In hardware-independent CEPC emulation environment, return the RET_NORMAL as the return value without recording the startup identifier.
197  * ARGUMENT  : u_int16  up_sts      :Startup Identifier Value
198  *           : int      reset       :Reset instruction
199  *           : u_int8  table    :Table Type
200  * RETURN    : RET_API defined
201  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
202 RET_API
203 _pb_UpManage(u_int32 up_sts, int reset, u_int8 table) {  // LCOV_EXCL_START 8:dead code
204     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
205     /* Is reset instructed? */
206     if ((reset == SYS_RESET) || (reset == SYS_ONLY_RESET)) {
207         PbReset();        /* Soft reset */
208     }
209
210     return RET_NORMAL;
211 }
212 // LCOV_EXCL_STOP
213
214 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
215  * MODULE    : _pb_RomcheckMask
216  * ABSTRACT  : Check the sum value of masked part
217  * NOTE      : Check the sum value of the mask ROM (BOOTROM)
218  * ARGUMENT  : None
219  * RETURN    : RET_API  RET_NORMAL    :Sum value normal
220  *                 REL_ERROR    :Sum value error
221  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
222 RET_API
223 _pb_RomcheckMask(void) {  // LCOV_EXCL_START 8:dead code
224     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
225     return(RET_NORMAL);
226 }
227 // LCOV_EXCL_STOP
228
229 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
230  * MODULE    : _pb_RomcheckApl
231  * ABSTRACT  : Check sum value of APL part (NK image)
232  * NOTE      : In hardware-independent CEPC emulations do nothing and return RET_NORMAL.
233  * ARGUMENT  :
234  * RETURN    : RET_API  RET_NORMAL    :Sum Value Normal
235  *                 REL_ERROR    :Sum value error
236  *            RET_OSERROR    :AccessPhysicalMem error
237  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
238 RET_API
239 _pb_RomcheckApl(void) {  // LCOV_EXCL_START 8:dead code
240     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
241     return(RET_NORMAL);
242 }
243 // LCOV_EXCL_STOP
244
245 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
246  * MODULE    : PbReset
247  * ABSTRACT  : Hard reset processing
248  * NOTE      : Manipulate the WDT control register to generate a hard reset
249  *           : Hardware-independent CEPC emulations do nothing and return RET_NORMAL.
250  * ARGUMENT  : void
251  * RETURN    : None
252  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
253 void
254 PbReset(void) {  // LCOV_EXCL_START 8:dead code
255     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
256
257     /* Message log output processing */
258     FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, " PbReset:%s:LINE %d\r\n #### HARD RESET !! ####\r\n",
259                    LTEXT(__FILE__), __LINE__);
260 }
261 // LCOV_EXCL_STOP
262
263 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
264  * MODULE    : _pb_GetDeviceInfo
265  * ABSTRACT  : Getting device information
266  * NOTE      : Gets device information into the specified buffer.
267  *           : In hardware-independent CEPC emulations, the following device
268  *           : information is returned permanently. The return value is always RET_NORMAL.
269  *           : _CWORD31__no="8611052060010001" mci_no="CNCS0200A" date="200301011200"
270  * ARGUMENT  : T_SYS_PRGVER     *pbuf   Destination buffer address
271  * RETURN    : RET_API defined
272  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
273 RET_API
274 _pb_GetDeviceInfo(T_SYS_DEVINFO *pbuf) {  // LCOV_EXCL_START 8:dead code
275     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
276     return(RET_NORMAL);
277 }
278 // LCOV_EXCL_STOP
279
280 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
281  * MODULE    : _pb_WriteDeviceInfo
282  * ABSTRACT  : Write device key information
283  * NOTE      : Write device key information to flash memory
284  * ARGUMENT  : u_char     *ucpData   Start address of write data (256 bytes)
285  * RETURN    : int32      VUP_SUCCESS      Flash write complete
286  *           :          VUP_FAILED      Flash write abend
287  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
288 int32
289 _pb_WriteDeviceInfo(u_char *ucpData) {  // LCOV_EXCL_START 8:dead code
290     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
291     return(RET_NORMAL);
292 }
293 // LCOV_EXCL_STOP
294
295 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
296  * MODULE    : _pb_GetSramVer
297  * ABSTRACT  : SRAM versioning process
298  * NOTE      : Getting SRAM version
299  *           : In hardware-independent CEPC emulations, return the following SRAM Version statically.
300  *           : The return value is always RET_NORMAL.
301  *           : SRAM Version=0x1112 1314
302  * ARGUMENT  : char     *pbuf       Destination buffer address
303  * RETURN    : RET_API defined
304  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
305 RET_API
306 _pb_GetSramVer(char *pbuf) {  // LCOV_EXCL_START 8:dead code
307     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
308     return(RET_NORMAL);
309 }
310 // LCOV_EXCL_STOP
311
312 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
313  * MODULE    : _pb_GetDisplayInfo
314  * ABSTRACT  : Getting display information
315  * NOTE      : Get the screen size (width and height) into the buffer of the DISPLAY_INFO structure
316  * ARGUMENT  : DISPLAY_INFO    *pSystemInfo  Screen size acquisition destination buffer address
317  * RETURN    : RET_API defined
318  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
319 #define DISPLAYINFO_SUBKEY_SIZE    56
320 #define DISPLAYINFO_VALNAME_SIZE  16
321 #define DISPLAYINFO_VAL_SIZE    8
322
323 RET_API
324 _pb_GetDisplayInfo(DISPLAY_INFO *pSystemInfo) {  // LCOV_EXCL_START 8:dead code
325     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
326 #ifdef PBCOM_CONFIGREAD
327     FILE*     fpConfigFile = NULL;            /* Configuration file handle    */
328     TCHAR    tcData[DISPLAYINFO_VAL_SIZE] = {0};      /* Data section read buffer  */
329     TCHAR    tcDataName[DISPLAYINFO_VALNAME_SIZE] = {0};  /* Data information storage buffer  */
330     int    iRet = PBCOM_API_NORMAL;          /* API return value        */
331 #endif
332     int    iHeight = 0;                /* Screen height information        */
333     int    iWidth = 0;                  /* Screen width information        */
334     RET_API  Retapi = RET_NORMAL;            /* Results of this Module process      */
335
336     /************************************************************
337      * Parameter check
338      ************************************************************/
339     if (NULL == pSystemInfo) {
340         /* Parameter is set to error */
341         Retapi = RET_ERRPARAM;
342     }
343
344     /************************************************************
345      * Set the information in the configuration file
346      ************************************************************/
347 #ifdef PBCOM_CONFIGREAD
348     /************************************************************
349      * Open configuration file
350      ************************************************************/
351     if (RET_NORMAL == Retapi) {
352         fpConfigFile = fopen(PBCOM_CONFIGFILE, "r");
353         if (NULL == fpConfigFile) {
354             /* Set error in as a result  */
355             Retapi = RET_ERROR;
356         }
357     }
358
359     /************************************************************
360      * Get screen width information from configuration file
361      ************************************************************/
362     if (RET_NORMAL == Retapi) {
363         /* Buffer clear  */
364         memset(tcData, 0x00, sizeof(tcData));
365         memset(tcDataName, 0x00, sizeof(tcDataName));
366
367         /* Data information setting  */
368         memcpy(tcDataName, PBCOM_CONFIG_INDEX_SCRWIDTH, strlen(PBCOM_CONFIG_INDEX_SCRWIDTH));
369
370         /************************************************************
371          * Setting file information reading process
372          ************************************************************/
373         Retapi = SysReadConfigInfo(fpConfigFile, tcDataName, tcData, (u_int32)sizeof(tcData));
374
375         /************************************************************
376          * Conversion process from character string (UNICODE) to numeric
377          ************************************************************/
378         if (RET_NORMAL == Retapi) {
379             /* Conversion from string to numeric  */
380             iHeight = SysUniToInt(tcData, strlen(tcData));
381         }
382     }
383
384     /************************************************************
385      * Get screen height information from setting file
386      ************************************************************/
387     if (RET_NORMAL == Retapi) {
388         /* Buffer clear  */
389         memset(tcData, 0x00, sizeof(tcData));
390         memset(tcDataName, 0x00, sizeof(tcDataName));
391
392         /* Data information setting  */
393         memcpy(tcDataName, PBCOM_CONFIG_INDEX_SCRHEIGHT, strlen(PBCOM_CONFIG_INDEX_SCRHEIGHT));
394
395         /************************************************************
396          * Setting file information reading process
397          ************************************************************/
398         Retapi = SysReadConfigInfo(fpConfigFile, tcDataName, tcData, (u_int32)sizeof(tcData));
399
400         /************************************************************
401          * Conversion process from character string (UNICODE) to numeric
402          ************************************************************/
403         if (RET_NORMAL == Retapi) {
404             /* Conversion from string to numeric */
405             iWidth = SysUniToInt(tcData, strlen(tcData));
406         }
407     }
408
409     /************************************************************
410      * Configuration file close
411      ************************************************************/
412     /* Executed only when the open is successful.  */
413     if (NULL != fpConfigFile) {
414         iRet = fclose(fpConfigFile);
415         if (PBCOM_API_NORMAL != iRet) {
416             Retapi = RET_ERROR;
417         }
418     }
419
420     /************************************************************
421      * Direct value setting
422      ************************************************************/
423 #else
424     iHeight = 480;
425     iWidth = 800;
426 #endif
427     /************************************************************
428      * Display information setting
429      ************************************************************/
430
431     if (RET_NORMAL == Retapi) {
432         /* Coverity CID: 18756 compliant */
433         pSystemInfo->i_height_size = iHeight;
434         pSystemInfo->i_width_size = iWidth;
435     } /* Coverity CID: 18756 compliant */
436
437     return Retapi;
438 }
439 // LCOV_EXCL_STOP
440
441 /******************************************************************************
442 * MODULE    : RomCheck
443 * ABSTRACT  : Calculate sum value within range of address specified by argument
444 * FUNCTION  : u_int32 RomCheck()
445 * ARGUMENT  : u_int32  *addr      Start address for which sum value is to be calculated
446 *             u_int32  check_size    Size for which the sum is to be calculated
447 * NOTE      :
448 * RETURN    : u_int32  sum        Sum value
449 ******************************************************************************/
450 u_int32
451 RomCheck(u_int32 *start_addr, u_int32 check_size) {  // LCOV_EXCL_START 8:dead code
452     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
453     u_int32     *addr_tmp;
454     u_int32     *end_addr;
455     u_int32     sum;
456
457     end_addr = start_addr + check_size / sizeof(u_int32);
458     addr_tmp = start_addr;
459
460     for (sum = 0; addr_tmp < end_addr;) {
461         sum += *(addr_tmp++);
462     }
463     return(sum);
464 }
465 // LCOV_EXCL_STOP
466
467 /******************************************************************************
468 * MODULE    : RomCheckEx
469 * ABSTRACT  : Calculate sum value in range of address on ROM specified by argument
470 * FUNCTION  : u_int32 RomCheckEx()
471 * ARGUMENT  : u_int32  *addr      Start address for which sum value is to be calculated
472 *             u_int32  check_size    Size for which the sum is to be calculated
473 * NOTE      : For single operation(For a one-bank configuration). APIs provided by FROM management
474 *              are used because FROM management must be exclusively controled.
475 *             The sum calculation unit is assumed to be the sector size of FlashROM.
476 * RETURN    : u_int32  sum        Sum value
477 *             When this function terminates with an error, "RET_ERROR" is returned.
478 ******************************************************************************/
479 u_int32
480 RomCheckEx(u_int32 *start_addr, u_int32 check_size) {  // LCOV_EXCL_START 8:dead code
481     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
482     return(0);
483 }
484 // LCOV_EXCL_STOP
485
486 /*******************************************************************************
487  * MODULE    : AtoxLong
488  * ABSTRACT    : Convert ASCII to hex
489  * FUNCTION    : Convert a ASCII string (up to 8 characters) to a uint64_t hexadecimal string
490  * NOTE      :
491  * RETURN    : uint64_t :Converted hexadecimal number (up to 8 digits)
492  * INPUT    : char *str -------------------- Pointer to a source ASCII string
493  *           unsigned int ascii_size ------ Number of characters in source ASCII string
494  * OUTPUT    :
495  ******************************************************************************/
496 uint64_t AtoxLong(char* str, unsigned int ascii_size) {  // LCOV_EXCL_START 8:dead code
497     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
498     uint64_t  x = 0;    /* Return value */
499     unsigned int  i;      /* For work */
500     unsigned int  tmp;    /* For work */
501
502     /* Check that the specified ASCII number of characters does not exceed the number of digits represented by int64_t (8 hexadecimal digits). */
503     if (ascii_size > (sizeof(x)) * 2) {
504         ascii_size = static_cast<unsigned int>(sizeof(x) * 2);
505     }
506     /* Convert one character at a time to hexadecimal */
507     for (i = 0; i < ascii_size; i++) {
508         /* Is a number? */
509         tmp = *str - '0';
510         if (tmp > 9) {
511             /* Coverity CID: 18790 compliant */
512             /* a-f? */
513             tmp = *str - 'a' + 10;
514             if ((tmp < 0x0a) || (tmp > 0x0f)) {
515                 /* A to F ? */
516                 tmp = *str - 'A' + 10;
517                 if ((tmp < 0x0a) || (tmp > 0x0f)) {
518                     /* Stop when a non-hexadecimal value is found */
519                     break;
520                 }
521             }
522         }
523
524         /* Increase the digit */
525         x <<= 4;
526         /* Add to the bottom digit */
527         x |= tmp;
528         str++;
529     }
530
531     return x;
532 }
533 // LCOV_EXCL_STOP
534
535 /*******************************************************************************
536  * MODULE    : XtoaLong
537  * ABSTRACT    : Convert HEX to ASCII
538  * FUNCTION    : This function converts the 8-digit hexadecimal number from str[0] to the ASCII code and stores the code.
539  * NOTE        : The string to be passed as *str must be at least 8 characters in length. * RETURN    :
540  * INPUT    : uint64_t x ------------ Source hexadecimal number
541  * OUTPUT    : char* str ------------------ Converted ASCII Strings (8 Characters)
542  ******************************************************************************/
543 void XtoaLong(uint64_t x, char* str) {  // LCOV_EXCL_START 8:dead code
544     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
545     int    i;      /* For looping */
546     int    size;    /* For looping */
547     char  tmp;    /* Conversion temporary buffer */
548
549     /* Maximum number of loops (for character string) */
550     size = static_cast<int>(sizeof(int64_t) * 2);
551     /* Convert eight digits */
552     for (i = 0; i < size; i++) {
553         tmp = static_cast<char>((x >> ((size - i - 1) * 4)) & 0x0F);
554
555         /* For 0xA to 0xF */
556         if (tmp > 0x09) {
557             str[i] = static_cast<char>('A' + tmp - 0x0A);
558         } else {
559             str[i] = static_cast<char>('0' + tmp);
560         }
561     }
562 }
563 // LCOV_EXCL_STOP
564
565 /******************************************************************************
566 * MODULE     : _pb_SendSystemError
567 * ABSTRACT   : System error notification
568 * FUNCTION   : Notify the system error and reset the software.
569 * ARGUMENT   : PNO   pno      Error process No.
570 *              int32 errcode  Error code
571 * NOTE       :   _pb_SndMsg : Send message (System API)
572 *                _sys_GetPno : PNO to be gotten
573 * RETURN     : RET_API defined
574 ******************************************************************************/
575 RET_API _pb_SendSystemError(PNO pno, int32 errcode) {  // LCOV_EXCL_START 8:dead code
576     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
577     T_SYS_SYSTEMERROR        msg;  /* Message         */
578     register  T_APIMSG_HEADER    *hdr;  /* Message header */
579     RET_API   api_ret;
580
581     /* Message Header Settings */
582     hdr = &(msg.bhead.hdr);
583
584     msg.bhead.signo    = 0;            /* Extension member            */
585     hdr->sndpno      = 0;                /* Source Process No.       */
586     hdr->respno      = 0;          /* Response destination process No.(None) */
587     hdr->cid      = CID_INI_SYSTEMERR;   /* Command ID              */
588     hdr->msgbodysize  = static_cast<uint16_t>(sizeof(T_SYS_SYSTEMERROR) - sizeof(T_APIMSG_MSGBUF_HEADER));
589     /* Main unit size              */
590     hdr->rid      = 0;          /* Resources ID (Dummy)      */
591     hdr->reserve    = 0;
592     msg.pno        = pno;          /* Error process No.       */
593     msg.errcode      = errcode;         /* Error code            */
594
595     /* Send Message */
596     api_ret = _pb_SndMsg(PNO_GINI_MAIN, sizeof(msg), &msg, 0);
597
598     return(api_ret);
599 }
600 // LCOV_EXCL_STOP
601
602 /******************************************************************************
603 * MODULE     : _pb_GetVupFileName
604 * ABSTRACT   : Getting the version upgrade file name
605 * FUNCTION   : Get the path name and file name of the version upgrade.
606 * ARGUMENT   : LPTSTR   filename      File name storage
607 *              u_char   media         Container media
608 * NOTE       :
609 * RETURN     : RET_API defined
610 ******************************************************************************/
611 RET_API _pb_GetVupFileName(LPTSTR filename , u_char media) {  // LCOV_EXCL_START 8:dead code
612     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
613     RET_API      api_ret = RET_NORMAL;  /* API return value */
614     T_SYS_PRGVER  ver;        /* Version Information */
615     RET_API      ret;        /* Internal function return value */
616     u_char      kisyu;        /* Storage of model code */
617
618     /* Get version */
619     ret = _pb_GetPrgVer(&ver);
620
621     if (ret == RET_NORMAL) {
622         /* Extract model code from version */
623         kisyu = ver.mask_ver[3];
624
625         /* If the media is a disk */
626         if (media == VUP_GETNAME_MEDIA_DISC) {
627             _stprintf(filename, _T("%s%s%s%02x%s"),
628                       VUP_DISC_PATHNAME, VUP_PATHNAME_SYSTEM, VUP_FNAME_TOP, kisyu, VUP_FNAME_BTM);
629         } else {
630             /* Media is USB */
631             _stprintf(filename, _T("%s%s%s%02x%s"),
632                       VUP_USB_PATHNAME, VUP_PATHNAME_SYSTEM, VUP_FNAME_TOP, kisyu, VUP_FNAME_BTM);
633         }
634     } else {
635         api_ret = RET_ERROR;
636     }
637
638     return(api_ret);
639 }
640 // LCOV_EXCL_STOP
641
642 /******************************************************************************
643 * MODULE     : _pb_GetForceVupFileName
644 * ABSTRACT   : Getting the version upgrade file name
645 * FUNCTION   : Get the path name and file name of the version upgrade.
646 * ARGUMENT   : LPTSTR   filename      File name storage
647 *              u_char   media         Container media
648 * NOTE       :
649 * RETURN     : RET_API defined
650 ******************************************************************************/
651 RET_API _pb_GetForceVupFileName(LPTSTR filename , u_char media) {  // LCOV_EXCL_START 8:dead code
652     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
653     RET_API      api_ret = RET_NORMAL;  /* API return value */
654
655     /* If the media is a disk */
656     if (media == VUP_GETNAME_MEDIA_DISC) {
657         _stprintf(filename, _T("%s%s%s"),
658                   VUP_DISC_PATHNAME, VUP_PATHNAME_SYSTEM, VUP_FNAME_FORCEVUP);
659     } else {
660         /* Media is USB */
661         _stprintf(filename, _T("%s%s%s"),
662                   VUP_USB_PATHNAME, VUP_PATHNAME_SYSTEM, VUP_FNAME_FORCEVUP);
663     }
664
665     return(api_ret);
666 }
667 // LCOV_EXCL_STOP
668
669 #ifdef PBCOM_CONFIGREAD
670 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
671  * MODULE    : SysGetConfigSize
672  * ABSTRACT  : Setting file size acquisition processing
673  * NOTE      : Get file size.
674  *           : When this Module is called, an area must be allocated at a higher level.
675  *           : Parameter checking is not performed in this Module.
676  * ARGUMENT  : FILE*  fpFile(File handle)
677  *           : int*    piSize(File size storage area)
678  * RETURN    : RET_API defined
679  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
680 static  RET_API
681 SysGetConfigSize(FILE* fpFile, u_int32* puiSize) {
682     int64_t     lCurrentPos = 0;          /* File current position  */
683     int64_t     lEndPos = 0;            /* End position of the file    */
684     int      iRet = PBCOM_API_NORMAL;      /* API processing result      */
685     RET_API    Retapi = RET_NORMAL;        /* Result of this Module process    */
686
687     /************************************************************
688      * Get current offset
689      ************************************************************/
690     lCurrentPos = ftell(fpFile);
691
692     /* If the return value is abnormal, the processing result is set to abnormal.  */
693     if (PBCOM_API_ERROR == lCurrentPos) {
694         Retapi = RET_ERROR;
695     }
696
697     /************************************************************
698      * Get end offset (size)
699      *   -Set offset to end-of-file
700      *   -Get end-of-file offset
701      ************************************************************/
702     if (RET_NORMAL == Retapi) {
703         /* Set offset to end-of-file  */
704         iRet = fseek(fpFile, 0L, SEEK_END);
705
706         /* If the return value is abnormal, the processing result is set to abnormal.  */
707         if (PBCOM_API_NORMAL != iRet) {
708             Retapi = RET_ERROR;
709         }
710     }
711
712     if (RET_NORMAL == Retapi) {
713         /* Get end-of-file offset */
714         lEndPos = ftell(fpFile);
715
716         /* Set file size if the return value is normal  */
717         if (PBCOM_API_ERROR != lEndPos) {
718             *puiSize = (u_int32)lEndPos;
719         } else {
720             /* If the return value is abnormal, the processing result is set to abnormal.  */
721             Retapi = RET_ERROR;
722         }
723     }
724
725     /************************************************************
726      * Revert current offset
727      ************************************************************/
728     if (RET_NORMAL == Retapi) {
729         /* Revert offset and complete processing */
730         iRet = fseek(fpFile, lCurrentPos, SEEK_SET);
731
732         /* If the return value is abnormal, the processing result is set to abnormal.  */
733         if (PBCOM_API_NORMAL != iRet) {
734             Retapi = RET_ERROR;
735         }
736     }
737
738     return Retapi;
739 }
740 #endif /* PBCOM_CONFIGREAD */
741
742 #ifdef PBCOM_CONFIGREAD
743 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
744  * MODULE    : SysReadConfigInfo
745  * ABSTRACT  : Setting file information reading process
746  * NOTE      : Read the data associated with the specified data information from the configuration file.
747  *           : When this module is called, an area must be allocated at a higher level.
748  *           : Parameter checking is not performed in this module.
749  * ARGUMENT  : FILE*  fpConfig(File handle)
750  *           : TCHAR*  tcDataIndex(Data information)
751  *           : TCHAR*  tcDataBuf(Data read buffer)
752  *           : int    tcDataSize(Data read size)
753  * RETURN    : RET_API defined
754  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
755 static RET_API
756 SysReadConfigInfo(FILE* fpConfig, TCHAR* tcDataIndex, TCHAR* tcDataValue, u_int32 tcDataSize) {
757     TCHAR  tcReadBuf[PBCOM_CONFIGSIZE_MAX] = {0};    /* Read buffer    */
758     u_int32  uiDataPos = 0;                /* Data position    */
759     u_int32  uiCpyPos = 0;                /* Copy position    */
760     u_int32  uiFileSize = 0;                /* File size    */
761     u_int32  uiReadSize = 0;                /* Data read size  */
762     u_int32  uiDataNum = 0;                /* Number of read data    */
763     int    iRet = 0;                  /* API processing result      */
764     RET_API  Retapi = RET_NORMAL;            /* Result of this Module process    */
765
766     /************************************************************
767      * Get setting file size
768      ************************************************************/
769     /* Get setting file size  */
770     Retapi = SysGetConfigSize(fpConfig, &uiFileSize);
771
772     /* When the setting file size is gotten successfully  */
773     if (RET_NORMAL == Retapi) {
774         /* The maximum size of the configuration file is greater than or equal to the maximum size of the configuration file.  */
775         if (uiFileSize > PBCOM_CONFIGSIZE_MAX) {
776             /* Set the maximum value of the configuration file as the size */
777             uiFileSize = PBCOM_CONFIGSIZE_MAX;
778         }
779     }
780
781     /************************************************************
782      * Read setting file
783      ************************************************************/
784     if (RET_NORMAL == Retapi) {
785         /* Read data count  */
786         uiDataNum = uiFileSize / sizeof(TCHAR);
787
788         /* Set file pointer to the beginning of file  */
789         rewind(fpConfig);
790
791         /* Read the data  */
792         uiReadSize = fread(tcReadBuf, sizeof(TCHAR), uiDataNum, fpConfig);
793
794         /* When the data corresponding to the file size cannot be read  */
795         if (uiReadSize != uiFileSize) {
796             /* Set error in processing result  */
797             Retapi = RET_ERROR;
798         }
799     }
800
801     /************************************************************
802      * Data information search & retrieval
803      *   -Find data information from the configuration file you read
804      *   -Read associated data if data information can be found 
805      ************************************************************/
806     if (RET_NORMAL == Retapi) {
807         /* Set error to processing result (Set normal when search is normal)  */
808         Retapi = RET_ERROR;
809
810         for (uiDataPos = 0; uiDataPos < uiDataNum; uiDataPos++) {
811             /* Determine file information  */
812             iRet = memcmp(&tcReadBuf[uiDataPos], tcDataIndex, strlen(tcDataIndex));
813
814             /************************************************************
815              * If the applicable data information(Data information matched)
816              ************************************************************/
817             if (0 == iRet) {
818                 /* Move to data position (Move file information)*/
819                 uiDataPos += strlen(tcDataIndex) / sizeof(TCHAR);
820
821                 /* Set normal to processing result for data discovery  */
822                 Retapi = RET_NORMAL;
823
824                 /************************************************************
825                  * Data read processing
826                  *   -Read data from the read data size
827                  *   -Assume that he end of the data has been reached when the following conditions are met
828                  *      NULL character code
829                  *      Line feed character code
830                  *      The data end of the configuration file is exceeded.
831                  ************************************************************/
832                 for (uiCpyPos = 0; uiCpyPos < tcDataSize; uiCpyPos++) {
833                     if (uiDataNum > uiDataPos) {
834                         /* Coverity CID: 18781 compliant */
835                         /* If it is valid data  */
836                         if ((PBCOM_UNICODE_NUL  != tcReadBuf[uiDataPos]) &&
837                                 (PBCOM_UNICODE_LF  != tcReadBuf[uiDataPos])) {
838                             /* Data setting*/
839                             tcDataValue[uiCpyPos] = tcReadBuf[uiDataPos];
840                         } else {
841                             /* Exit from this loop  */
842                             break;
843                         }
844                     } else {
845                       /* When the end of data is reached  */
846                         /* Exit from this loop  */
847                         break;
848                     }
849                     /* Move the data position by the set amount  */
850                     uiDataPos++;
851                 }
852                 /************************************************************
853                  * Completion of data information search
854                  ************************************************************/
855                 break;
856             } else {
857                 /************************************************************
858                  * Not applicable data information(Data information mismatch)
859                  ************************************************************/
860                 /************************************************************
861                  * Move to next data information
862                  *   -Determine supine data if following condition is met
863                  *      Line feed character code
864                  *   -End processing when the end of file is reached
865                  ************************************************************/
866                 while (uiDataPos < uiDataNum) {
867                     /* Data determination  */
868                     if (PBCOM_UNICODE_LF == tcReadBuf[uiDataPos]) {
869                         /*Next Data Detection  */
870                         break;
871                     }
872                     /* Move the data by the determined amount  */
873                     uiDataPos++;
874                 }
875             }
876         }
877     }
878     return Retapi;
879 }
880 #endif /* PBCOM_CONFIGREAD */
881
882 #ifdef PBCOM_CONFIGREAD
883 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
884  * MODULE    : SysUniToInt
885  * ABSTRACT  : Converting from Unicode(string) to Int(number)
886  * NOTE      : Convert from Unicode(string) to Int(number).
887  *           : The number of characters that can be converted is defined in PBCOM_UTOIMAX_NUM.
888  *           : When this module is called, an area must be allocated at a higher level.
889  *           : Parameter checking is not performed in this module.
890  * ARGUMENT  : TCHAR*  tcData(Character string storage area)
891  *           : int    iDataSize(Character array size)
892  * RETURN    : Number converted from character string
893  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
894 static int SysUniToInt(TCHAR* tcData, u_int32 iDataSize) {
895     int    iValue = 0;            /* Converted value    */
896     u_int8  ucCount = 0;          /* Character string data position  */
897
898     /************************************************************
899      * Parameter check
900      ************************************************************/
901     if (PBCOM_UTOIMAX_NUM < iDataSize) {
902         iDataSize = PBCOM_UTOIMAX_NUM;
903     }
904
905     /************************************************************
906      * Conversion from string to number
907      ************************************************************/
908     for (ucCount = 0; ucCount < iDataSize; ucCount++) {
909         /*If it is a number  */
910         if ((tcData[ucCount] >= '0') &&
911                 (tcData[ucCount] <= '9')) {
912             iValue = (iValue * 10) + (tcData[ucCount] - '0');
913         } else {
914           /* If it is not a digit  */
915             /* End of data  */
916             break;
917         }
918     }
919     return iValue;
920 }
921 #endif /* PBCOM_CONFIGREAD */
922
923 /*
924 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
925  End of File : _sysCommon.cpp
926 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
927 */
928