WIP: Shell: improve cpuid - view full brand string #304

Draft
rgimad wants to merge 1 commits from shell-improve-cpuid into main
4 changed files with 246 additions and 238 deletions

1
.gitignore vendored
View File

@@ -9,3 +9,4 @@ ehthumbs_vista.db
._*
programs/cmm/cmm.code-workspace
programs/cmm/menu/.gitignore
.vscode

View File

@@ -26,8 +26,8 @@ int cmd_kfetch(char param[]) {
char str_resolution[24];
ksys_pos_t resol = _ksys_screen_size();
sprintf(str_resolution, "%u x %u", resol.x + 1, resol.y + 1);
char str_cpu_info[16];
get_str_cpu_info(str_cpu_info);
char str_cpu_info[50];
Review

Magic number

Magic number
get_cpu_brand_string(str_cpu_info);
char str_meminfo[24];
get_str_meminfo(str_meminfo);
@@ -53,7 +53,7 @@ int cmd_kfetch(char param[]) {
"\033[0;34;40m \033[0;31;40m \033[0;32;40mS\033[0;34;40m@\033[0;5;34;40mX\033[0;1;35;45m.\033[0;1;34;44m8\033[0;5;35;45m \033[0;5;34;44m \033[0;1;30;45m8\033[0;5;35;44m:\033[0;5;37;45m@\033[0;1;30;40m8\033[0;31;40m.\033[0;34;40m \033[0;31;40m \033[0;34;40m \033[0m\n\r"
"\033[0;34;40m \033[0;31;40m \033[0;32;40m \033[0;31;40m.\033[0;32;40m;\033[0;34;40m;8\033[0;32;40m%%\033[0;5;34;40m8\033[0;34;40m8\033[0;1;30;44m8\033[0;1;30;40m8\033[0;34;40m;.\033[0;31;40m \033[0;34;40m \033[0;31;40m \033[0;34;40m \033[0m\n\r"
"\033[0;34;40m \033[0;32;40m \033[0;31;40m \033[0;32;40m.\033[0;31;40m.\033[0;32;40m.\033[0;31;40m.\033[0;32;40m.\033[0;31;40m:. \033[0;32;40m. \033[0;34;40m \033[0;31;40m \033[0;34;40m \033[0m\n\r",
"\033[0;36mOS\033[0m: KolibriOS ", str_os_rel_offset_dbgtag, "\033[0;36mKernel\033[0m: ", str_krn_abi_cmtid, "\033[0;36mUptime\033[0m: ", str_uptime, "\033[0;36mResolution\033[0m: ", str_resolution, "\033[0;36mCPU\033[0m: ", str_cpu_info, "\033[0;36mMemory\033[0m: ", str_meminfo
);

View File

@@ -1,65 +1,72 @@
#include "../system/kolibri.h"
void get_str_kernel_version(char *str, const char *fmt) {
struct kernel_version kv;
kol_get_kernel_ver(&kv);
char str_offset[8] = {'\0'};
if (kv.offset)
sprintf(str_offset, "+%u", kv.offset);
char str_dbgtag[4] = {'\0'};
if (kv.dbgtag)
sprintf(str_dbgtag, "-%c", kv.dbgtag);
char str_cmtid[16] = {'\0'};
if (kv.cmtid)
sprintf(str_cmtid, " (%08x)", kv.cmtid);
sprintf(str, fmt, kv.osrel[0], kv.osrel[1], kv.osrel[2], kv.osrel[3],
str_offset, str_dbgtag, str_cmtid, kv.abimaj, kv.abimin);
}
void get_str_cpu_info(char *str) {
unsigned a, b, c, d;
asm ("cpuid" :
"=a" (a),
"=b" (b),
"=c" (c),
"=d" (d):
"a"(0));
str[0] = (b&0x000000ff) >> 0;
str[1] = (b&0x0000ff00) >> 8;
str[2] = (b&0x00ff0000) >> 16;
str[3] = (b&0xff000000) >> 24;
str[4] = (d&0x000000ff) >> 0;
str[5] = (d&0x0000ff00) >> 8;
str[6] = (d&0x00ff0000) >> 16;
str[7] = (d&0xff000000) >> 24;
str[8] = (c&0x000000ff) >> 0;
str[9] = (c&0x0000ff00) >> 8;
str[10] = (c&0x00ff0000) >> 16;
str[11] = (c&0xff000000) >> 24;
str[12] = '\0';
}
int cmd_ver(char param[]) {
if (!strcmp(param, "kernel")) {
get_str_kernel_version(tmpstr, CMD_VER_FMT1);
printf(tmpstr);
return TRUE;
}
if (!strcmp(param, "cpu")) {
char str[13];
get_str_cpu_info(str);
printf("%s\n\r", str);
return TRUE;
}
printf (" Shell v%s\n\r", SHELL_VERSION);
return TRUE;
}
#include "../system/kolibri.h"
void get_str_kernel_version(char *str, const char *fmt) {
struct kernel_version kv;
kol_get_kernel_ver(&kv);
char str_offset[8] = {'\0'};
if (kv.offset)
sprintf(str_offset, "+%u", kv.offset);
char str_dbgtag[4] = {'\0'};
if (kv.dbgtag)
sprintf(str_dbgtag, "-%c", kv.dbgtag);
char str_cmtid[16] = {'\0'};
if (kv.cmtid)
sprintf(str_cmtid, " (%08x)", kv.cmtid);
sprintf(str, fmt, kv.osrel[0], kv.osrel[1], kv.osrel[2], kv.osrel[3],
str_offset, str_dbgtag, str_cmtid, kv.abimaj, kv.abimin);
}
// Retrieves the CPU Brand String and writes it to the provided buffer.
// out_buffer: A buffer of at least 49 bytes (48 for string + 1 for null)
void get_cpu_brand_string(char *out_buffer) {
if (!out_buffer) return;
uint32_t regs[4];
// 1. Check maximum extended function support
__asm__ (
"cpuid"
: "=a" (regs[0]), "=b" (regs[1]), "=c" (regs[2]), "=d" (regs[3])
: "a" (0x80000000)
);
if (regs[0] < 0x80000004) {
strcpy(out_buffer, "Brand String Not Supported");
return;
}
// 2. Extract brand string from leaves 0x80000002 to 0x80000004
for (uint32_t leaf = 0x80000002; leaf <= 0x80000004; leaf++) {
__asm__ (
"cpuid"
: "=a" (regs[0]), "=b" (regs[1]), "=c" (regs[2]), "=d" (regs[3])
: "a" (leaf)
);
// Copy the 16 bytes (4 registers * 4 bytes) from this leaf into the buffer
memcpy(out_buffer + (leaf - 0x80000002) * 16, regs, 16);
}
out_buffer[48] = '\0';
Review

Magic number

Magic number
}
int cmd_ver(char param[]) {
if (!strcmp(param, "kernel")) {
get_str_kernel_version(tmpstr, CMD_VER_FMT1);
printf(tmpstr);
return TRUE;
}
if (!strcmp(param, "cpu")) {
char str[49];
Review

Magic number

Magic number
get_cpu_brand_string(str);
printf("%s\n\r", str);
return TRUE;
}
printf (" Shell v%s\n\r", SHELL_VERSION);
return TRUE;
}

View File

@@ -1,170 +1,170 @@
Review

You changed either the file encoding or its EOL format.

You changed either the file encoding or its EOL format.
Review

The original file had CFRLF eol, and some trailing whitespaces... I'd guess both are fixed automatically in vscode, etc.

@mxlgv - since the only change is whitespace, are you proposing it is excluded from the PR? -Or- a separate Commit for 'white space cleanup' ...?

The original file had `CFRLF` eol, and some trailing whitespaces... I'd guess both are fixed automatically in vscode, etc. @mxlgv - since the only change is whitespace, are you proposing it is excluded from the PR? -Or- a separate Commit for 'white space cleanup' ...?
Review

@ace-dent Hi. I am of the opinion that the fewer changes the better). Spaces may need to be removed, but EOL should be left alone...

It can be changed to “more modern” but not specifically mixed with these changes

@ace-dent Hi. I am of the opinion that the fewer changes the better). Spaces may need to be removed, but EOL should be left alone... It can be changed to “more modern” but not specifically mixed with these changes
#include "all.h"
int dir_check(char dir[])
/// just checks, if dir[] is really a directory
{
kol_struct70 k70;
int result;
k70.p00 = 1;
k70.p04 = 0;
//k70.p08 = 0;
k70.p12 = 2; // enough to read . & ..
k70.p16 = (unsigned)malloc(32+k70.p12*560);
k70.p20 = 0;
k70.p21 = dir;
result = kol_file_70(&k70);
free((void*)k70.p16);
if ( (0 == result)||(6 == result) ) // 6 is possible ???
return TRUE;
else
return FALSE;
}
void dir_truncate(char dir[])
{
int i;
i = strlen(dir)-1;
for (;;i--)
if ('/' == dir[i])
{
dir[i+1] = 0;
break;
}
}
void get_file_dir_loc(char *filepath, char *dir_path)
{
char *res = strrchr(filepath, '/');
if (res == 0)
{
dir_path = '\0';
return;
}
size_t pos = res - filepath;
strncpy(dir_path, filepath, pos);
dir_path[pos] = '\0';
}
int file_check(char file[])
{
kol_struct70 k70;
int result;
k70.p00 = 0;
k70.p04 = 0;
//k70.p08 = 0;
k70.p12 = 0;
k70.p16 = 0;
k70.p20 = 0;
k70.p21 = file;
result = kol_file_70(&k70);
if (0 == result)
return TRUE;
else
return FALSE;
}
void file_not_found(char file[]) {
printf (FILE_NOT_FOUND_ERROR, file);
}
int iswhite(char c) {return ((' ' == c) || ('\t' == c) || (13 == c) || (10 == c)); }
void trim(char string[])
{
int i, j;
for (i=0; ;i++)
if ( !iswhite(string[i]) )
break;
j = 0;
for (;;i++, j++)
{
string[j] = string[i];
if ('\0' == string[i] )
break;
}
for (i=0; ;i++)
if ('\0' == string[i])
break;
i--;
for (;i>0;--i)
if ( iswhite(string[i]) )
string[i] = '\0';
else
break;
}
// entry point
int main(int argc, char **argv)
{
int i; for (i = 1; i < argc; i++) {
strcat(cmdline, argv[i]);
if (i != argc - 1) {
strcat(cmdline, " ");
}
}
NUM_OF_CMD = sizeof(COMMANDS)/sizeof(COMMANDS[0]);
strcpy(title, "SHELL ");
strcat(title, SHELL_VERSION);
con_init_opt(-1, -1, -1, -1, title);
//printf("argc = %d\ncmdline = '%s'\n", argc, cmdline);
if (sizeof (kol_struct70) != 25) {
printf("Invalid struct align kol_struct70, need to fix compile options\n\r");
kol_exit();
}
//strcpy(cur_dir, PATH);
//dir_truncate(cur_dir);
getcwd(cur_dir, sizeof cur_dir);
//printf("curdir %s\n", cur_dir);
con_set_cursor_height(con_get_font_height()-1);
ALIASES = malloc(128*1024);
if (!cmdline || cmdline[0] == 0) {
strcpy(CMD, argv[0]);
dir_truncate(CMD);
strcat(CMD, ".shell");
if ( !file_check(CMD) )
strcpy(CMD, "/sys/settings/.shell");
}
else {
if (cmdline[0] == '/')
{
strcpy(cur_dir, cmdline);
*(strrchr(cur_dir, '/')+1)=0;
}
strcpy(CMD, cmdline);
}
command_execute();
for (;;) {
//printf("\033[32;1m");
printf ("# ");
//printf("\033[0m");
command_get();
command_execute();
}
con_exit(0);
kol_exit();
}
#include "all.h"
int dir_check(char dir[])
/// just checks, if dir[] is really a directory
{
kol_struct70 k70;
int result;
k70.p00 = 1;
k70.p04 = 0;
//k70.p08 = 0;
k70.p12 = 2; // enough to read . & ..
k70.p16 = (unsigned)malloc(32+k70.p12*560);
k70.p20 = 0;
k70.p21 = dir;
result = kol_file_70(&k70);
free((void*)k70.p16);
if ( (0 == result)||(6 == result) ) // 6 is possible ???
return TRUE;
else
return FALSE;
}
void dir_truncate(char dir[])
{
int i;
i = strlen(dir)-1;
for (;;i--)
if ('/' == dir[i])
{
dir[i+1] = 0;
break;
}
}
void get_file_dir_loc(char *filepath, char *dir_path)
{
char *res = strrchr(filepath, '/');
if (res == 0)
{
dir_path = '\0';
return;
}
size_t pos = res - filepath;
strncpy(dir_path, filepath, pos);
dir_path[pos] = '\0';
}
int file_check(char file[])
{
kol_struct70 k70;
int result;
k70.p00 = 0;
k70.p04 = 0;
//k70.p08 = 0;
k70.p12 = 0;
k70.p16 = 0;
k70.p20 = 0;
k70.p21 = file;
result = kol_file_70(&k70);
if (0 == result)
return TRUE;
else
return FALSE;
}
void file_not_found(char file[]) {
printf (FILE_NOT_FOUND_ERROR, file);
}
int iswhite(char c) {return ((' ' == c) || ('\t' == c) || (13 == c) || (10 == c)); }
void trim(char string[])
{
int i, j;
for (i=0; ;i++)
if ( !iswhite(string[i]) )
break;
j = 0;
for (;;i++, j++)
{
string[j] = string[i];
if ('\0' == string[i] )
break;
}
for (i=0; ;i++)
if ('\0' == string[i])
break;
i--;
for (;i>0;--i)
if ( iswhite(string[i]) )
string[i] = '\0';
else
break;
}
// entry point
int main(int argc, char **argv)
{
int i; for (i = 1; i < argc; i++) {
strcat(cmdline, argv[i]);
if (i != argc - 1) {
strcat(cmdline, " ");
}
}
NUM_OF_CMD = sizeof(COMMANDS)/sizeof(COMMANDS[0]);
strcpy(title, "SHELL ");
strcat(title, SHELL_VERSION);
con_init_opt(-1, -1, -1, -1, title);
//printf("argc = %d\ncmdline = '%s'\n", argc, cmdline);
if (sizeof (kol_struct70) != 25) {
printf("Invalid struct align kol_struct70, need to fix compile options\n\r");
kol_exit();
}
//strcpy(cur_dir, PATH);
//dir_truncate(cur_dir);
getcwd(cur_dir, sizeof cur_dir);
//printf("curdir %s\n", cur_dir);
con_set_cursor_height(con_get_font_height()-1);
ALIASES = malloc(128*1024);
if (!cmdline || cmdline[0] == 0) {
strcpy(CMD, argv[0]);
dir_truncate(CMD);
strcat(CMD, ".shell");
if ( !file_check(CMD) )
strcpy(CMD, "/sys/settings/.shell");
}
else {
if (cmdline[0] == '/')
{
strcpy(cur_dir, cmdline);
*(strrchr(cur_dir, '/')+1)=0;
}
strcpy(CMD, cmdline);
}
command_execute();
for (;;) {
//printf("\033[32;1m");
printf ("# ");
//printf("\033[0m");
command_get();
command_execute();
}
con_exit(0);
kol_exit();
}