/* * Copyright (c) Hisilicon Technologies Co., Ltd. 2020-2020. All rights reserved. * Description: common module * Author: Hisilicon * Create: 2020-10-15 */ #include "uapi_flash.h" #include "nand.h" #include "flash_ext.h" #include "securec.h" /* change length to string */ td_char *int_to_size(td_u64 size) { td_s32 ix; td_u64 temp_size = size; static td_char buffer[20]; /* 20 - buffer size */ const td_char *fmt[] = {" ", "K", "M", "G", "T", "T"}; for (ix = 0; (ix < 5) && ((temp_size & 0x3FF) == 0) && (temp_size != 0); ix++) { /* 5 - left shift bits */ temp_size = (temp_size >> 10); /* 10 - right shift bits */ } if (snprintf_s(buffer, sizeof(buffer), sizeof(buffer) - 1, "%u%s", (td_u32)temp_size, fmt[ix]) == -1) { dbg_out("Failed to snprintf_s.\n"); return NULL; } return buffer; } /* * Modified for CONNAX CA. * When /dev path has no excute permission, stat call will fail. * Try parse /proc/mtd to get max partition number. */ td_s32 get_max_partition(td_void) { FILE *fd = NULL; #define MAX_PROC_LINE_SZ 1024 td_char bf[MAX_PROC_LINE_SZ]; td_s32 nr = 0; fd = fopen(PROC_MTD_FILE, "r"); if (fd == NULL) { dbg_out("Fail to open %s!\n", PROC_MTD_FILE); return -1; } /* skip first prompt line */ if (!fgets(bf, MAX_PROC_LINE_SZ, fd)) { (td_void)fclose(fd); return -1; } while (fgets(bf, MAX_PROC_LINE_SZ, fd) != 0) { nr++; } (td_void)fclose(fd); dbg_out("max partition nr %d\n", nr); /* keep the way before do */ return nr - 1; } td_s32 offshift(td_ulong n) { td_s32 shift = -1; td_ulong num = n; while (num != 0) { num = num >> 1; shift++; } return shift; } td_char *skip_space(td_char *line) { td_char *p = NULL; if (line == NULL) { return NULL; } p = line; while (*p == ' ' || *p == '\t') { p++; } return p; } td_char *skip_word(td_char *line) { td_char *p = NULL; if (line == NULL) { return NULL; } p = line; while (*p != '\t' && *p != ' ' && *p != '\n' && *p != 0) { p++; } return p; } td_char *get_word(td_char *line, td_char *value) { td_char *p = NULL; td_char *word = value; if ((line == NULL) || (value == NULL)) { return NULL; } p = line; p = skip_space(p); while (*p != '\t' && *p != ' ' && *p != '\n' && *p != 0) { *word++ = *p++; } *word = 0; return p; } td_s32 get_bootargs(td_u8 *bootargs, td_u16 length) { FILE *pf = NULL; if (bootargs == NULL) { dbg_out("Pointer is null.\n"); return -1; } pf = fopen("/proc/cmdline", "r"); if (pf == NULL) { dbg_out("Failed to open '/proc/cmdline'.\n"); return -1; } if (!fgets((td_char*)bootargs, length, pf)) { dbg_out("Failed to fgets string.\n"); (td_void)fclose(pf); return -1; } (td_void)fclose(pf); return 0; }