1/******************************************************************************* 2Copyright (C) Marvell International Ltd. and its affiliates 3 4This software file (the "File") is owned and distributed by Marvell 5International Ltd. and/or its affiliates ("Marvell") under the following 6alternative licensing terms. Once you have made an election to distribute the 7File under one of the following license alternatives, please (i) delete this 8introductory statement regarding license alternatives, (ii) delete the two 9license alternatives that you have not elected to use and (iii) preserve the 10Marvell copyright notice above. 11 12******************************************************************************** 13Marvell Commercial License Option 14 15If you received this File from Marvell and you have entered into a commercial 16license agreement (a "Commercial License") with Marvell, the File is licensed 17to you under the terms of the applicable Commercial License. 18 19******************************************************************************** 20Marvell GPL License Option 21 22If you received this File from Marvell, you may opt to use, redistribute and/or 23modify this File in accordance with the terms and conditions of the General 24Public License Version 2, June 1991 (the "GPL License"), a copy of which is 25available along with the File in the license.txt file or by writing to the Free 26Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 or 27on the worldwide web at http://www.gnu.org/licenses/gpl.txt. 28 29THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE IMPLIED 30WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY 31DISCLAIMED. The GPL License provides additional details about this warranty 32disclaimer. 33******************************************************************************** 34Marvell BSD License Option 35 36If you received this File from Marvell, you may opt to use, redistribute and/or 37modify this File under the following licensing terms. 38Redistribution and use in source and binary forms, with or without modification, 39are permitted provided that the following conditions are met: 40 41 * Redistributions of source code must retain the above copyright notice, 42 this list of conditions and the following disclaimer. 43 44 * Redistributions in binary form must reproduce the above copyright 45 notice, this list of conditions and the following disclaimer in the 46 documentation and/or other materials provided with the distribution. 47 48 * Neither the name of Marvell nor the names of its contributors may be 49 used to endorse or promote products derived from this software without 50 specific prior written permission. 51 52THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 53ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 54WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 55DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 56ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 57(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 58LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 59ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 60(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 61SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 62 63*******************************************************************************/ 64 65#include "mvOs.h" 66#include "mv802_3.h" 67#include "mvCommon.h" 68 69 70/******************************************************************************* 71* mvMacStrToHex - Convert MAC format string to hex. 72* 73* DESCRIPTION: 74* This function convert MAC format string to hex. 75* 76* INPUT: 77* macStr - MAC address string. Fornat of address string is 78* uu:vv:ww:xx:yy:zz, where ":" can be any delimiter. 79* 80* OUTPUT: 81* macHex - MAC in hex format. 82* 83* RETURN: 84* None. 85* 86*******************************************************************************/ 87MV_STATUS mvMacStrToHex(const char* macStr, MV_U8* macHex) 88{ 89 int i; 90 char tmp[3]; 91 92 for(i = 0; i < MV_MAC_ADDR_SIZE; i++) 93 { 94 tmp[0] = macStr[(i * 3) + 0]; 95 tmp[1] = macStr[(i * 3) + 1]; 96 tmp[2] = '\0'; 97 macHex[i] = (MV_U8) (strtol(tmp, NULL, 16)); 98 } 99 return MV_OK; 100} 101 102/******************************************************************************* 103* mvMacHexToStr - Convert MAC in hex format to string format. 104* 105* DESCRIPTION: 106* This function convert MAC in hex format to string format. 107* 108* INPUT: 109* macHex - MAC in hex format. 110* 111* OUTPUT: 112* macStr - MAC address string. String format is uu:vv:ww:xx:yy:zz. 113* 114* RETURN: 115* None. 116* 117*******************************************************************************/ 118MV_STATUS mvMacHexToStr(MV_U8* macHex, char* macStr) 119{ 120 int i; 121 122 for(i = 0; i < MV_MAC_ADDR_SIZE; i++) 123 { 124 mvOsSPrintf(&macStr[i * 3], "%02x:", macHex[i]); 125 } 126 macStr[(i * 3) - 1] = '\0'; 127 128 return MV_OK; 129} 130 131/******************************************************************************* 132* mvSizePrint - Print the given size with size unit description. 133* 134* DESCRIPTION: 135* This function print the given size with size unit description. 136* FOr example when size paramter is 0x180000, the function prints: 137* "size 1MB+500KB" 138* 139* INPUT: 140* size - Size in bytes. 141* 142* OUTPUT: 143* None. 144* 145* RETURN: 146* None. 147* 148*******************************************************************************/ 149MV_VOID mvSizePrint(MV_U32 size) 150{ 151 mvOsOutput("size "); 152 153 if(size >= _1G) 154 { 155 mvOsOutput("%3dGB ", size / _1G); 156 size %= _1G; 157 if(size) 158 mvOsOutput("+"); 159 } 160 if(size >= _1M ) 161 { 162 mvOsOutput("%3dMB ", size / _1M); 163 size %= _1M; 164 if(size) 165 mvOsOutput("+"); 166 } 167 if(size >= _1K) 168 { 169 mvOsOutput("%3dKB ", size / _1K); 170 size %= _1K; 171 if(size) 172 mvOsOutput("+"); 173 } 174 if(size > 0) 175 { 176 mvOsOutput("%3dB ", size); 177 } 178} 179 180/******************************************************************************* 181* mvHexToBin - Convert hex to binary 182* 183* DESCRIPTION: 184* This function Convert hex to binary. 185* 186* INPUT: 187* pHexStr - hex buffer pointer. 188* size - Size to convert. 189* 190* OUTPUT: 191* pBin - Binary buffer pointer. 192* 193* RETURN: 194* None. 195* 196*******************************************************************************/ 197MV_VOID mvHexToBin(const char* pHexStr, MV_U8* pBin, int size) 198{ 199 int j, i; 200 char tmp[3]; 201 MV_U8 byte; 202 203 for(j=0, i=0; j<size; j++, i+=2) 204 { 205 tmp[0] = pHexStr[i]; 206 tmp[1] = pHexStr[i+1]; 207 tmp[2] = '\0'; 208 byte = (MV_U8) (strtol(tmp, NULL, 16) & 0xFF); 209 pBin[j] = byte; 210 } 211} 212 213void mvAsciiToHex(const char* asciiStr, char* hexStr) 214{ 215 int i=0; 216 217 while(asciiStr[i] != 0) 218 { 219 mvOsSPrintf(&hexStr[i*2], "%02x", asciiStr[i]); 220 i++; 221 } 222 hexStr[i*2] = 0; 223} 224 225 226void mvBinToHex(const MV_U8* bin, char* hexStr, int size) 227{ 228 int i; 229 230 for(i=0; i<size; i++) 231 { 232 mvOsSPrintf(&hexStr[i*2], "%02x", bin[i]); 233 } 234 hexStr[i*2] = '\0'; 235} 236 237void mvBinToAscii(const MV_U8* bin, char* asciiStr, int size) 238{ 239 int i; 240 241 for(i=0; i<size; i++) 242 { 243 mvOsSPrintf(&asciiStr[i*2], "%c", bin[i]); 244 } 245 asciiStr[i*2] = '\0'; 246} 247 248/******************************************************************************* 249* mvLog2 - 250* 251* DESCRIPTION: 252* Calculate the Log2 of a given number. 253* 254* INPUT: 255* num - A number to calculate the Log2 for. 256* 257* OUTPUT: 258* None. 259* 260* RETURN: 261* Log 2 of the input number, or 0xFFFFFFFF if input is 0. 262* 263*******************************************************************************/ 264MV_U32 mvLog2(MV_U32 num) 265{ 266 MV_U32 result = 0; 267 if(num == 0) 268 return 0xFFFFFFFF; 269 while(num != 1) 270 { 271 num = num >> 1; 272 result++; 273 } 274 return result; 275} 276 277 278