1 /*
2  * uuidutil.c
3  *
4  * DSP-BIOS Bridge driver support functions for TI OMAP processors.
5  *
6  * This file contains the implementation of UUID helper functions.
7  *
8  * Copyright (C) 2005-2006 Texas Instruments, Inc.
9  *
10  * This package is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  *
14  * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
16  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17  */
18 #include <linux/types.h>
19 
20 /*  ----------------------------------- Host OS */
21 #include <dspbridge/host_os.h>
22 
23 /*  ----------------------------------- DSP/BIOS Bridge */
24 #include <dspbridge/dbdefs.h>
25 
26 /*  ----------------------------------- This */
27 #include <dspbridge/uuidutil.h>
28 
29 /*
30  *  ======== uuid_uuid_to_string ========
31  *  Purpose:
32  *      Converts a struct dsp_uuid to a string.
33  *      Note: snprintf format specifier is:
34  *      %[flags] [width] [.precision] [{h | l | I64 | L}]type
35  */
uuid_uuid_to_string(struct dsp_uuid * uuid_obj,char * sz_uuid,s32 size)36 void uuid_uuid_to_string(struct dsp_uuid *uuid_obj, char *sz_uuid,
37 			 s32 size)
38 {
39 	s32 i;			/* return result from snprintf. */
40 
41 	i = snprintf(sz_uuid, size,
42 		     "%.8X_%.4X_%.4X_%.2X%.2X_%.2X%.2X%.2X%.2X%.2X%.2X",
43 		     uuid_obj->data1, uuid_obj->data2, uuid_obj->data3,
44 		     uuid_obj->data4, uuid_obj->data5,
45 		     uuid_obj->data6[0], uuid_obj->data6[1],
46 		     uuid_obj->data6[2], uuid_obj->data6[3],
47 		     uuid_obj->data6[4], uuid_obj->data6[5]);
48 }
49 
uuid_hex_to_bin(char * buf,s32 len)50 static s32 uuid_hex_to_bin(char *buf, s32 len)
51 {
52 	s32 i;
53 	s32 result = 0;
54 	int value;
55 
56 	for (i = 0; i < len; i++) {
57 		value = hex_to_bin(*buf++);
58 		result *= 16;
59 		if (value > 0)
60 			result += value;
61 	}
62 
63 	return result;
64 }
65 
66 /*
67  *  ======== uuid_uuid_from_string ========
68  *  Purpose:
69  *      Converts a string to a struct dsp_uuid.
70  */
uuid_uuid_from_string(char * sz_uuid,struct dsp_uuid * uuid_obj)71 void uuid_uuid_from_string(char *sz_uuid, struct dsp_uuid *uuid_obj)
72 {
73 	s32 j;
74 
75 	uuid_obj->data1 = uuid_hex_to_bin(sz_uuid, 8);
76 	sz_uuid += 8;
77 
78 	/* Step over underscore */
79 	sz_uuid++;
80 
81 	uuid_obj->data2 = (u16) uuid_hex_to_bin(sz_uuid, 4);
82 	sz_uuid += 4;
83 
84 	/* Step over underscore */
85 	sz_uuid++;
86 
87 	uuid_obj->data3 = (u16) uuid_hex_to_bin(sz_uuid, 4);
88 	sz_uuid += 4;
89 
90 	/* Step over underscore */
91 	sz_uuid++;
92 
93 	uuid_obj->data4 = (u8) uuid_hex_to_bin(sz_uuid, 2);
94 	sz_uuid += 2;
95 
96 	uuid_obj->data5 = (u8) uuid_hex_to_bin(sz_uuid, 2);
97 	sz_uuid += 2;
98 
99 	/* Step over underscore */
100 	sz_uuid++;
101 
102 	for (j = 0; j < 6; j++) {
103 		uuid_obj->data6[j] = (u8) uuid_hex_to_bin(sz_uuid, 2);
104 		sz_uuid += 2;
105 	}
106 }
107