|
| 1 | +/* * Copyright (c) 2014, 2015 Zhang Xianyi |
| 2 | + * All rights reserved. |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without modification, |
| 5 | + * are permitted provided that the following conditions are met: |
| 6 | + * |
| 7 | + * * Redistributions of source code must retain the above copyright notice, this |
| 8 | + * list of conditions and the following disclaimer. |
| 9 | + * |
| 10 | + * * Redistributions in binary form must reproduce the above copyright notice, this |
| 11 | + * list of conditions and the following disclaimer in the documentation and/or |
| 12 | + * other materials provided with the distribution. |
| 13 | + * |
| 14 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 15 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 16 | + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 17 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
| 18 | + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 19 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 20 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 21 | + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 23 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | + */ |
| 25 | + |
| 26 | +#include <stdio.h> |
| 27 | +#include <string.h> |
| 28 | + |
| 29 | +#define CPUNAME_GENERIC 0 |
| 30 | +#define CPUNAME_ARMV7 1 |
| 31 | +#define CPUNAME_CORTEXA9 2 |
| 32 | +#define CPUNAME_CORTEXA15 3 |
| 33 | + |
| 34 | +static char *cpuname[] = { |
| 35 | + "generic", |
| 36 | + "armv7a", |
| 37 | + "cortexa9", |
| 38 | + "cortexa15", |
| 39 | +}; |
| 40 | + |
| 41 | +int get_feature(char *search) |
| 42 | +{ |
| 43 | + FILE *infile; |
| 44 | + char buffer[2048], *p,*t; |
| 45 | + p = (char *) NULL; |
| 46 | + |
| 47 | + infile = fopen("/proc/cpuinfo", "r"); |
| 48 | + if (infile == NULL) { |
| 49 | + return 0; |
| 50 | + } |
| 51 | + |
| 52 | + while (fgets(buffer, sizeof(buffer), infile)) { |
| 53 | + if (!strncmp("Features", buffer, 8)) { |
| 54 | + p = strchr(buffer, ':') + 2; |
| 55 | + break; |
| 56 | + } |
| 57 | + } |
| 58 | + fclose(infile); |
| 59 | + |
| 60 | + if( p == NULL ) return 0; |
| 61 | + |
| 62 | + t = strtok(p," "); |
| 63 | + if (t != NULL) { |
| 64 | + if (!strcmp(t, search)) { return 1; } |
| 65 | + } |
| 66 | + while( t = strtok(NULL," ")){ |
| 67 | + if (!strcmp(t, search)) { return 1; } |
| 68 | + } |
| 69 | + |
| 70 | + return 0; |
| 71 | +} |
| 72 | + |
| 73 | +int cpu_detect(void) |
| 74 | +{ |
| 75 | + FILE *infile; |
| 76 | + char buffer[512], *p; |
| 77 | + p = (char *) NULL ; |
| 78 | + |
| 79 | + infile = fopen("/proc/cpuinfo", "r"); |
| 80 | + if (infile == NULL) { |
| 81 | + return CPUNAME_GENERIC; |
| 82 | + } |
| 83 | + while (fgets(buffer, sizeof(buffer), infile)) { |
| 84 | + if (!strncmp("CPU part", buffer, 8)) { |
| 85 | + p = strchr(buffer, ':') + 2; |
| 86 | + break; |
| 87 | + } |
| 88 | + } |
| 89 | + fclose(infile); |
| 90 | + |
| 91 | + if(p != NULL) { |
| 92 | + if (strstr(p, "0xc09")) { |
| 93 | + if(get_feature("neon")) |
| 94 | + return CPUNAME_CORTEXA9; |
| 95 | + else |
| 96 | + return CPUNAME_ARMV7; |
| 97 | + } |
| 98 | + if (strstr(p, "0xc0f")) { |
| 99 | + if(get_feature("neon")) |
| 100 | + return CPUNAME_CORTEXA15; |
| 101 | + else |
| 102 | + return CPUNAME_ARMV7; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + p = (char *) NULL ; |
| 107 | + infile = fopen("/proc/cpuinfo", "r"); |
| 108 | + if (infile == NULL) { |
| 109 | + return CPUNAME_GENERIC; |
| 110 | + } |
| 111 | + |
| 112 | + while (fgets(buffer, sizeof(buffer), infile)) { |
| 113 | + if ((!strncmp("model name", buffer, 10)) || (!strncmp("Processor", buffer, 9))) { |
| 114 | + p = strchr(buffer, ':') + 2; |
| 115 | + break; |
| 116 | + } |
| 117 | + } |
| 118 | + fclose(infile); |
| 119 | + |
| 120 | + if(p != NULL) { |
| 121 | + if (strstr(p, "ARMv7")) { |
| 122 | + return CPUNAME_ARMV7; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + return CPUNAME_GENERIC; |
| 127 | +} |
| 128 | + |
| 129 | +int main() |
| 130 | +{ |
| 131 | + int cpuname_id; |
| 132 | + |
| 133 | + cpuname_id=cpu_detect(); |
| 134 | + printf("%s", cpuname[cpuname_id]); |
| 135 | + return 0; |
| 136 | +} |
0 commit comments