@@ -868,46 +868,39 @@ static int uuid_from_dmi_entries(char *system_uuid)
868868 * uuid_from_product_uuid() - Get system UUID from product_uuid
869869 * @system_uuid: Where to save the system UUID.
870870 *
871- * Get system UUID from /sys/class/dmi/id/product_uuid and fix
872- * endianess.
873- *
874871 * Return: 0 on success, -ENXIO otherwise.
875872 */
876873static int uuid_from_product_uuid (char * system_uuid )
877874{
878- FILE * stream = NULL ;
879- int ret = - ENXIO ;
875+ FILE * stream ;
876+ ssize_t nread ;
877+ int ret ;
878+ char * line = NULL ;
879+ size_t len = 0 ;
880880
881+ stream = fopen (PATH_DMI_PROD_UUID , "re" );
882+ if (!stream )
883+ return - ENXIO ;
881884 system_uuid [0 ] = '\0' ;
882885
883- if ((stream = fopen (PATH_DMI_PROD_UUID , "re" )) != NULL ) {
884- char * line = NULL ;
885- size_t len = 0 ;
886- ssize_t nread = getline (& line , & len , stream );
887-
888- if (nread == UUID_SIZE ) {
889- /* Per "DMTF SMBIOS 3.0 Section 7.2.1 System UUID", the
890- * UUID retrieved from the DMI has the wrong endianess.
891- * The following copies "line" to "system_uuid" while
892- * swapping from little-endian to network-endian. */
893- static const int swaptbl [] = {
894- 6 ,7 ,4 ,5 ,2 ,3 ,0 ,1 ,8 ,11 ,12 ,9 ,10 ,13 ,16 ,17 ,14 ,15 ,18 ,19 ,
895- 20 ,21 ,22 ,23 ,24 ,25 ,26 ,27 ,28 ,29 ,30 ,31 ,32 ,33 ,34 ,35 ,
896- -1 /* sentinel */
897- };
898- int i ;
899-
900- for (i = 0 ; swaptbl [i ] != -1 ; i ++ )
901- system_uuid [i ] = line [swaptbl [i ]];
902- system_uuid [UUID_SIZE - 1 ] = '\0' ;
903-
904- ret = 0 ;
905- }
906-
907- free (line );
908- fclose (stream );
886+ nread = getline (& line , & len , stream );
887+ if (nread != UUID_SIZE ) {
888+ ret = - ENXIO ;
889+ goto out ;
909890 }
910891
892+ /* The kernel is handling the byte swapping according DMTF
893+ * SMBIOS 3.0 Section 7.2.1 System UUID */
894+
895+ memcpy (system_uuid , line , UUID_SIZE - 1 );
896+ system_uuid [UUID_SIZE - 1 ] = '\0' ;
897+
898+ ret = 0 ;
899+
900+ out :
901+ free (line );
902+ fclose (stream );
903+
911904 return ret ;
912905}
913906
0 commit comments