1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187
|
/*
* Copyright 2016 Richard Hughes <richard@hughsie.com>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#define G_LOG_DOMAIN "FuPlugin"
#include "config.h"
#include <errno.h>
#include <fcntl.h>
#include <fwupd.h>
#include <gmodule.h>
#include <string.h>
#include <unistd.h>
#include "fu-bytes.h"
#include "fu-config-private.h"
#include "fu-context-private.h"
#include "fu-device-private.h"
#include "fu-kernel.h"
#include "fu-path.h"
#include "fu-plugin-private.h"
#include "fu-security-attr.h"
#include "fu-string.h"
/**
* FuPlugin:
*
* A plugin which is used by fwupd to enumerate and update devices.
*
* See also: [class@FuDevice], [class@Fwupd.Plugin]
*/
static void
fu_plugin_finalize(GObject *object);
typedef struct {
GModule *module;
guint order;
guint priority;
gboolean done_init;
GPtrArray *rules[FU_PLUGIN_RULE_LAST];
GPtrArray *devices; /* (nullable) (element-type FuDevice) */
GHashTable *runtime_versions;
GHashTable *compile_versions;
FuContext *ctx;
GArray *device_gtypes; /* (nullable): of #GType */
GType device_gtype_default;
GHashTable *cache; /* (nullable): platform_id:GObject */
GHashTable *report_metadata; /* (nullable): key:value */
GFileMonitor *config_monitor;
FuPluginData *data;
FuPluginVfuncs vfuncs;
} FuPluginPrivate;
enum { PROP_0, PROP_CONTEXT, PROP_LAST };
enum {
SIGNAL_DEVICE_ADDED,
SIGNAL_DEVICE_REMOVED,
SIGNAL_DEVICE_REGISTER,
SIGNAL_RULES_CHANGED,
SIGNAL_CHECK_SUPPORTED,
SIGNAL_LAST
};
static guint signals[SIGNAL_LAST] = {0};
G_DEFINE_TYPE_WITH_PRIVATE(FuPlugin, fu_plugin, FWUPD_TYPE_PLUGIN)
#define GET_PRIVATE(o) (fu_plugin_get_instance_private(o))
typedef void (*FuPluginInitVfuncsFunc)(FuPluginVfuncs *vfuncs);
typedef gboolean (*FuPluginDeviceFunc)(FuPlugin *self, FuDevice *device, GError **error);
typedef gboolean (*FuPluginDeviceProgressFunc)(FuPlugin *self,
FuDevice *device,
FuProgress *progress,
GError **error);
typedef gboolean (*FuPluginFlaggedDeviceFunc)(FuPlugin *self,
FuDevice *device,
FuProgress *progress,
FwupdInstallFlags flags,
GError **error);
typedef gboolean (*FuPluginDeviceArrayFunc)(FuPlugin *self, GPtrArray *devices, GError **error);
/**
* fu_plugin_is_open:
* @self: a #FuPlugin
*
* Determines if the plugin is opened
*
* Returns: TRUE for opened, FALSE for not
*
* Since: 1.3.5
**/
gboolean
fu_plugin_is_open(FuPlugin *self)
{
FuPluginPrivate *priv = GET_PRIVATE(self);
return priv->module != NULL;
}
/**
* fu_plugin_get_name:
* @self: a #FuPlugin
*
* Gets the plugin name.
*
* Returns: a plugin name, or %NULL for unknown.
*
* Since: 0.8.0
**/
const gchar *
fu_plugin_get_name(FuPlugin *self)
{
g_return_val_if_fail(FU_IS_PLUGIN(self), NULL);
return fwupd_plugin_get_name(FWUPD_PLUGIN(self));
}
/**
* fu_plugin_set_name:
* @self: a #FuPlugin
* @name: a string
*
* Sets the plugin name.
*
* Since: 0.8.0
**/
void
fu_plugin_set_name(FuPlugin *self, const gchar *name)
{
FuPluginPrivate *priv = GET_PRIVATE(self);
g_return_if_fail(FU_IS_PLUGIN(self));
g_return_if_fail(!priv->done_init);
if (g_strcmp0(name, fwupd_plugin_get_name(FWUPD_PLUGIN(self))) == 0) {
g_critical("plugin name set to original value: %s", name);
return;
}
if (fwupd_plugin_get_name(FWUPD_PLUGIN(self)) != NULL) {
g_debug("overwriting plugin name %s -> %s",
fwupd_plugin_get_name(FWUPD_PLUGIN(self)),
name);
}
fwupd_plugin_set_name(FWUPD_PLUGIN(self), name);
}
static FuPluginVfuncs *
fu_plugin_get_vfuncs(FuPlugin *self)
{
FuPluginPrivate *priv = GET_PRIVATE(self);
if (fu_plugin_has_flag(self, FWUPD_PLUGIN_FLAG_MODULAR))
return &priv->vfuncs;
return FU_PLUGIN_GET_CLASS(self);
}
/**
* fu_plugin_cache_lookup:
* @self: a #FuPlugin
* @id: the key
*
* Finds an object in the per-plugin cache.
*
* Returns: (transfer none): a #GObject, or %NULL for unfound.
*
* Since: 0.8.0
**/
gpointer
fu_plugin_cache_lookup(FuPlugin *self, const gchar *id)
{
FuPluginPrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FU_IS_PLUGIN(self), NULL);
g_return_val_if_fail(id != NULL, NULL);
if (priv->cache == NULL)
return NULL;
return g_hash_table_lookup(priv->cache, id);
}
/**
* fu_plugin_cache_add:
* @self: a #FuPlugin
* @id: the key
* @dev: a #GObject, typically a #FuDevice
*
* Adds an object to the per-plugin cache.
*
* Since: 0.8.0
**/
void
fu_plugin_cache_add(FuPlugin *self, const gchar *id, gpointer dev)
{
FuPluginPrivate *priv = GET_PRIVATE(self);
g_return_if_fail(FU_IS_PLUGIN(self));
g_return_if_fail(id != NULL);
g_return_if_fail(G_IS_OBJECT(dev));
if (priv->cache == NULL) {
priv->cache = g_hash_table_new_full(g_str_hash,
g_str_equal,
g_free,
(GDestroyNotify)g_object_unref);
}
g_hash_table_insert(priv->cache, g_strdup(id), g_object_ref(dev));
}
/**
* fu_plugin_cache_remove:
* @self: a #FuPlugin
* @id: the key
*
* Removes an object from the per-plugin cache.
*
* Since: 0.8.0
**/
void
fu_plugin_cache_remove(FuPlugin *self, const gchar *id)
{
FuPluginPrivate *priv = GET_PRIVATE(self);
g_return_if_fail(FU_IS_PLUGIN(self));
g_return_if_fail(id != NULL);
if (priv->cache == NULL)
return;
if (g_hash_table_remove(priv->cache, id))
g_debug("removed %s object %s", fu_plugin_get_name(self), id);
}
/**
* fu_plugin_get_data:
* @self: a #FuPlugin
*
* Gets the per-plugin allocated private data. This will return %NULL unless
* fu_plugin_alloc_data() has been called by the plugin.
*
* Returns: (transfer none): a pointer to a structure, or %NULL for unset.
*
* Since: 0.8.0
**/
FuPluginData *
fu_plugin_get_data(FuPlugin *self)
{
FuPluginPrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FU_IS_PLUGIN(self), NULL);
return priv->data;
}
/**
* fu_plugin_alloc_data: (skip):
* @self: a #FuPlugin
* @data_sz: the size to allocate
*
* Allocates the per-plugin allocated private data.
*
* Returns: (transfer full): a pointer to a structure, or %NULL for unset.
*
* Since: 0.8.0
**/
FuPluginData *
fu_plugin_alloc_data(FuPlugin *self, gsize data_sz)
{
FuPluginPrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FU_IS_PLUGIN(self), NULL);
if (priv->data != NULL) {
g_critical("fu_plugin_alloc_data() already used by plugin");
return priv->data;
}
priv->data = g_malloc0(data_sz);
return priv->data;
}
/**
* fu_plugin_guess_name_from_fn:
* @filename: filename to guess
*
* Tries to guess the name of the plugin from a filename
*
* Returns: (transfer full): the guessed name of the plugin
*
* Since: 1.0.8
**/
gchar *
fu_plugin_guess_name_from_fn(const gchar *filename)
{
const gchar *prefix = "libfu_plugin_";
gchar *name;
gchar *str = g_strstr_len(filename, -1, prefix);
if (str == NULL)
return NULL;
name = g_strdup(str + strlen(prefix));
g_strdelimit(name, ".", '\0');
return name;
}
/**
* fu_plugin_open:
* @self: a #FuPlugin
* @filename: the shared object filename to open
* @error: (nullable): optional return location for an error
*
* Opens the plugin module, and calls `->load()` on it.
*
* Returns: TRUE for success, FALSE for fail
*
* Since: 0.8.0
**/
gboolean
fu_plugin_open(FuPlugin *self, const gchar *filename, GError **error)
{
FuPluginPrivate *priv = GET_PRIVATE(self);
FuPluginVfuncs *vfuncs;
FuPluginInitVfuncsFunc init_vfuncs = NULL;
g_return_val_if_fail(FU_IS_PLUGIN(self), FALSE);
g_return_val_if_fail(filename != NULL, FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
priv->module = g_module_open(filename, 0);
if (priv->module == NULL) {
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_NOT_SUPPORTED,
"failed to open plugin %s: %s",
filename,
g_module_error());
fu_plugin_add_flag(self, FWUPD_PLUGIN_FLAG_FAILED_OPEN);
fu_plugin_add_flag(self, FWUPD_PLUGIN_FLAG_USER_WARNING);
return FALSE;
}
/* call the vfunc setup */
g_module_symbol(priv->module, "fu_plugin_init_vfuncs", (gpointer *)&init_vfuncs);
if (init_vfuncs == NULL) {
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_INTERNAL,
"failed to init_vfuncs() on plugin %s",
filename);
fu_plugin_add_flag(self, FWUPD_PLUGIN_FLAG_FAILED_OPEN);
fu_plugin_add_flag(self, FWUPD_PLUGIN_FLAG_USER_WARNING);
return FALSE;
}
/* we can't "fallback" from modular to built-in so this is safe */
fu_plugin_add_flag(self, FWUPD_PLUGIN_FLAG_MODULAR);
vfuncs = fu_plugin_get_vfuncs(self);
init_vfuncs(vfuncs);
/* set automatically */
if (fu_plugin_get_name(self) == NULL) {
g_autofree gchar *str = fu_plugin_guess_name_from_fn(filename);
fu_plugin_set_name(self, str);
}
/* optional */
if (vfuncs->load != NULL) {
FuContext *ctx = fu_plugin_get_context(self);
g_debug("load(%s)", filename);
vfuncs->load(ctx);
}
return TRUE;
}
/**
* fu_plugin_add_string:
* @self: a #FuPlugin
* @idt: indent level
* @str: a string to append to
*
* Add daemon-specific device metadata to an existing string.
*
* Since: 1.8.4
**/
void
fu_plugin_add_string(FuPlugin *self, guint idt, GString *str)
{
FuPluginPrivate *priv = GET_PRIVATE(self);
FuPluginVfuncs *vfuncs = fu_plugin_get_vfuncs(self);
g_return_if_fail(FU_IS_PLUGIN(self));
g_return_if_fail(str != NULL);
/* attributes */
fwupd_codec_add_string(FWUPD_CODEC(self), idt, str);
fwupd_codec_string_append_int(str, idt + 1, "Order", priv->order);
fwupd_codec_string_append_int(str, idt + 1, "Priority", priv->priority);
if (priv->device_gtype_default != G_TYPE_INVALID) {
fwupd_codec_string_append(str,
idt + 1,
"DeviceGTypeDefault",
g_type_name(priv->device_gtype_default));
}
/* optional */
if (vfuncs->to_string != NULL)
vfuncs->to_string(self, idt + 1, str);
}
/**
* fu_plugin_to_string:
* @self: a #FuPlugin
*
* This allows us to easily print the plugin metadata.
*
* Returns: a string value, or %NULL for invalid.
*
* Since: 1.8.4
**/
gchar *
fu_plugin_to_string(FuPlugin *self)
{
g_autoptr(GString) str = g_string_new(NULL);
g_return_val_if_fail(FU_IS_PLUGIN(self), NULL);
fu_plugin_add_string(self, 0, str);
return g_string_free(g_steal_pointer(&str), FALSE);
}
/* order of usefulness to the user */
static const gchar *
fu_plugin_build_device_update_error(FuPlugin *self)
{
if (fu_plugin_has_flag(self, FWUPD_PLUGIN_FLAG_NO_HARDWARE))
return "Not updatable as required hardware was not found";
if (fu_plugin_has_flag(self, FWUPD_PLUGIN_FLAG_LEGACY_BIOS))
return "Not updatable in legacy BIOS mode";
if (fu_plugin_has_flag(self, FWUPD_PLUGIN_FLAG_CAPSULES_UNSUPPORTED))
return "Not updatable as UEFI capsule updates not enabled in firmware setup";
if (fu_plugin_has_flag(self, FWUPD_PLUGIN_FLAG_UNLOCK_REQUIRED))
return "Not updatable as requires unlock";
if (fu_plugin_has_flag(self, FWUPD_PLUGIN_FLAG_AUTH_REQUIRED))
return "Not updatable as requires authentication";
if (fu_plugin_has_flag(self, FWUPD_PLUGIN_FLAG_EFIVAR_NOT_MOUNTED))
return "Not updatable as efivarfs was not found";
if (fu_plugin_has_flag(self, FWUPD_PLUGIN_FLAG_ESP_NOT_FOUND))
return "Not updatable as UEFI ESP partition not detected";
if (fu_plugin_has_flag(self, FWUPD_PLUGIN_FLAG_DISABLED))
return "Not updatable as plugin was disabled";
return NULL;
}
static void
fu_plugin_ensure_devices(FuPlugin *self)
{
FuPluginPrivate *priv = GET_PRIVATE(self);
if (priv->devices != NULL)
return;
priv->devices = g_ptr_array_new_with_free_func((GDestroyNotify)g_object_unref);
}
static void
fu_plugin_device_child_added_cb(FuDevice *device, FuDevice *child, FuPlugin *self)
{
g_debug("child %s added to parent %s after setup, adding to daemon",
fu_device_get_id(child),
fu_device_get_id(device));
fu_plugin_device_add(self, child);
}
static void
fu_plugin_device_child_removed_cb(FuDevice *device, FuDevice *child, FuPlugin *self)
{
g_debug("child %s removed from parent %s after setup, removing from daemon",
fu_device_get_id(child),
fu_device_get_id(device));
fu_plugin_device_remove(self, child);
}
/**
* fu_plugin_device_add:
* @self: a #FuPlugin
* @device: a device
*
* Asks the daemon to add a device to the exported list. If this device ID
* has already been added by a different plugin then this request will be
* ignored.
*
* Since: 0.8.0
**/
void
fu_plugin_device_add(FuPlugin *self, FuDevice *device)
{
FuPluginPrivate *priv = GET_PRIVATE(self);
GPtrArray *children;
g_autoptr(GError) error = NULL;
g_return_if_fail(FU_IS_PLUGIN(self));
g_return_if_fail(FU_IS_DEVICE(device));
/* make tests easier */
fu_device_convert_instance_ids(device);
/* ensure the device ID is set from the physical and logical IDs */
if (!fu_device_ensure_id(device, &error)) {
g_warning("ignoring add: %s", error->message);
return;
}
/* add to array */
fu_plugin_ensure_devices(self);
g_ptr_array_add(priv->devices, g_object_ref(device));
/* proxy to device where required */
if (fu_plugin_has_flag(self, FWUPD_PLUGIN_FLAG_CLEAR_UPDATABLE)) {
if (fu_plugin_has_flag(self, FWUPD_PLUGIN_FLAG_USER_WARNING)) {
fu_device_inhibit(device,
"clear-updatable",
fu_plugin_build_device_update_error(self));
} else {
fu_device_inhibit(device,
"clear-updatable",
"Plugin disallowed updates with no user warning");
}
}
g_debug("emit added from %s: %s", fu_plugin_get_name(self), fu_device_get_id(device));
if (fu_device_get_created_usec(device) == 0)
fu_device_set_created_usec(device, g_get_real_time());
fu_device_set_plugin(device, fu_plugin_get_name(self));
g_signal_emit(self, signals[SIGNAL_DEVICE_ADDED], 0, device);
/* add children if they have not already been added */
children = fu_device_get_children(device);
for (guint i = 0; i < children->len; i++) {
FuDevice *child = g_ptr_array_index(children, i);
if (fu_device_get_created_usec(child) == 0)
fu_plugin_device_add(self, child);
}
/* watch to see if children are added or removed at runtime */
g_signal_connect(FU_DEVICE(device),
"child-added",
G_CALLBACK(fu_plugin_device_child_added_cb),
self);
g_signal_connect(FU_DEVICE(device),
"child-removed",
G_CALLBACK(fu_plugin_device_child_removed_cb),
self);
}
/**
* fu_plugin_get_devices:
* @self: a #FuPlugin
*
* Returns all devices added by the plugin using [method@FuPlugin.device_add] and
* not yet removed with [method@FuPlugin.device_remove].
*
* Returns: (transfer none) (element-type FuDevice): devices
*
* Since: 1.5.6
**/
GPtrArray *
fu_plugin_get_devices(FuPlugin *self)
{
FuPluginPrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FU_IS_PLUGIN(self), NULL);
fu_plugin_ensure_devices(self);
return priv->devices;
}
/**
* fu_plugin_device_register:
* @self: a #FuPlugin
* @device: a device
*
* Registers the device with other plugins so they can set metadata.
*
* Plugins do not have to call this manually as this is done automatically
* when using [method@FuPlugin.device_add]. They may wish to use this manually
* if for instance the coldplug should be ignored based on the metadata
* set from other plugins.
*
* Since: 0.9.7
**/
void
fu_plugin_device_register(FuPlugin *self, FuDevice *device)
{
g_autoptr(GError) error = NULL;
g_return_if_fail(FU_IS_PLUGIN(self));
g_return_if_fail(FU_IS_DEVICE(device));
/* ensure the device ID is set from the physical and logical IDs */
if (!fu_device_ensure_id(device, &error)) {
g_warning("ignoring registration: %s", error->message);
return;
}
g_debug("emit device-register from %s: %s",
fu_plugin_get_name(self),
fu_device_get_id(device));
g_signal_emit(self, signals[SIGNAL_DEVICE_REGISTER], 0, device);
}
/**
* fu_plugin_device_remove:
* @self: a #FuPlugin
* @device: a device
*
* Asks the daemon to remove a device from the exported list.
*
* Since: 0.8.0
**/
void
fu_plugin_device_remove(FuPlugin *self, FuDevice *device)
{
FuPluginPrivate *priv = GET_PRIVATE(self);
g_return_if_fail(FU_IS_PLUGIN(self));
g_return_if_fail(FU_IS_DEVICE(device));
g_debug("emit removed from %s: %s", fu_plugin_get_name(self), fu_device_get_id(device));
g_signal_emit(self, signals[SIGNAL_DEVICE_REMOVED], 0, device);
/* remove from array */
if (priv->devices != NULL)
g_ptr_array_remove(priv->devices, device);
}
/**
* fu_plugin_check_supported:
* @self: a #FuPlugin
* @guid: a hardware ID GUID, e.g. `6de5d951-d755-576b-bd09-c5cf66b27234`
*
* Checks to see if a specific device GUID is supported, i.e. available in the
* AppStream metadata.
*
* Returns: %TRUE if the device is supported.
*
* Since: 1.0.0
**/
static gboolean
fu_plugin_check_supported(FuPlugin *self, const gchar *guid)
{
gboolean retval = FALSE;
g_signal_emit(self, signals[SIGNAL_CHECK_SUPPORTED], 0, guid, &retval);
return retval;
}
/**
* fu_plugin_get_context:
* @self: a #FuPlugin
*
* Gets the context for a plugin.
*
* Returns: (transfer none): a #FuContext or %NULL if not set
*
* Since: 1.6.0
**/
FuContext *
fu_plugin_get_context(FuPlugin *self)
{
FuPluginPrivate *priv = GET_PRIVATE(self);
return priv->ctx;
}
/**
* fu_plugin_set_context:
* @self: a #FuPlugin
* @ctx: (nullable): optional #FuContext
*
* Sets the context for this plugin.
*
* Since: 1.8.6
**/
void
fu_plugin_set_context(FuPlugin *self, FuContext *ctx)
{
FuPluginPrivate *priv = GET_PRIVATE(self);
g_return_if_fail(FU_IS_PLUGIN(self));
g_return_if_fail(FU_IS_CONTEXT(ctx) || ctx == NULL);
if (g_set_object(&priv->ctx, ctx))
g_object_notify(G_OBJECT(self), "context");
}
static gboolean
fu_plugin_device_attach(FuPlugin *self, FuDevice *device, FuProgress *progress, GError **error)
{
FuDevice *proxy = fu_device_get_proxy_with_fallback(device);
FuDeviceClass *device_class = FU_DEVICE_GET_CLASS(proxy);
g_autoptr(FuDeviceLocker) locker = NULL;
if (device_class->attach == NULL)
return TRUE;
locker = fu_device_locker_new(proxy, error);
if (locker == NULL)
return FALSE;
return fu_device_attach_full(device, progress, error);
}
static gboolean
fu_plugin_device_detach(FuPlugin *self, FuDevice *device, FuProgress *progress, GError **error)
{
FuDevice *proxy = fu_device_get_proxy_with_fallback(device);
FuDeviceClass *device_class = FU_DEVICE_GET_CLASS(proxy);
g_autoptr(FuDeviceLocker) locker = NULL;
if (device_class->detach == NULL)
return TRUE;
locker = fu_device_locker_new(proxy, error);
if (locker == NULL)
return FALSE;
return fu_device_detach_full(device, progress, error);
}
static gboolean
fu_plugin_device_activate(FuPlugin *self, FuDevice *device, FuProgress *progress, GError **error)
{
FuDevice *proxy = fu_device_get_proxy_with_fallback(device);
FuDeviceClass *device_class = FU_DEVICE_GET_CLASS(proxy);
g_autoptr(FuDeviceLocker) locker = NULL;
if (device_class->activate == NULL)
return TRUE;
locker = fu_device_locker_new(proxy, error);
if (locker == NULL)
return FALSE;
return fu_device_activate(device, progress, error);
}
static gboolean
fu_plugin_device_write_firmware(FuPlugin *self,
FuDevice *device,
FuFirmware *firmware,
FuProgress *progress,
FwupdInstallFlags flags,
GError **error)
{
FuDevice *proxy = fu_device_get_proxy_with_fallback(device);
g_autoptr(FuDeviceLocker) locker = NULL;
locker = fu_device_locker_new(proxy, error);
if (locker == NULL) {
g_prefix_error_literal(error, "failed to open device: ");
return FALSE;
}
/* back the old firmware up to /var/lib/fwupd */
if (fu_device_has_flag(device, FWUPD_DEVICE_FLAG_BACKUP_BEFORE_INSTALL)) {
g_autoptr(GBytes) fw_old = NULL;
g_autofree gchar *path = NULL;
g_autofree gchar *fn = NULL;
/* progress */
fu_progress_set_id(progress, G_STRLOC);
fu_progress_add_flag(progress, FU_PROGRESS_FLAG_NO_PROFILE);
fu_progress_add_step(progress, FWUPD_STATUS_DEVICE_READ, 25, NULL);
fu_progress_add_step(progress, FWUPD_STATUS_DEVICE_WRITE, 75, NULL);
fw_old = fu_device_dump_firmware(device, fu_progress_get_child(progress), error);
if (fw_old == NULL) {
g_prefix_error_literal(error, "failed to backup old firmware: ");
return FALSE;
}
fn = g_strdup_printf("%s.bin", fu_device_get_version(device));
path = fu_path_build(
FU_PATH_KIND_LOCALSTATEDIR_PKG,
"backup",
fu_device_get_id(device),
fu_device_get_serial(device) != NULL ? fu_device_get_serial(device) : "default",
fn,
NULL);
fu_progress_step_done(progress);
if (!fu_bytes_set_contents(path, fw_old, error))
return FALSE;
if (!fu_device_write_firmware(device,
firmware,
fu_progress_get_child(progress),
flags,
error))
return FALSE;
fu_progress_step_done(progress);
return TRUE;
}
return fu_device_write_firmware(device, firmware, progress, flags, error);
}
static gboolean
fu_plugin_device_get_results(FuPlugin *self, FuDevice *device, GError **error)
{
g_autoptr(FuDeviceLocker) locker = NULL;
g_autoptr(GError) error_local = NULL;
locker = fu_device_locker_new(device, error);
if (locker == NULL)
return FALSE;
if (!fu_device_get_results(device, &error_local)) {
if (g_error_matches(error_local, FWUPD_ERROR, FWUPD_ERROR_NOT_SUPPORTED))
return TRUE;
g_propagate_error(error, g_steal_pointer(&error_local));
return FALSE;
}
return TRUE;
}
static gboolean
fu_plugin_device_read_firmware(FuPlugin *self,
FuDevice *device,
FuProgress *progress,
GError **error)
{
FuDevice *proxy = fu_device_get_proxy_with_fallback(device);
g_autoptr(FuDeviceLocker) locker = NULL;
g_autoptr(FuFirmware) firmware = NULL;
g_autoptr(GBytes) fw = NULL;
GChecksumType checksum_types[] = {G_CHECKSUM_SHA1, G_CHECKSUM_SHA256, 0};
locker = fu_device_locker_new(proxy, error);
if (locker == NULL)
return FALSE;
if (!fu_device_detach_full(device, progress, error))
return FALSE;
firmware = fu_device_read_firmware(device, progress, FU_FIRMWARE_PARSE_FLAG_NONE, error);
if (firmware == NULL) {
g_autoptr(GError) error_local = NULL;
if (!fu_device_attach_full(device, progress, &error_local))
g_debug("ignoring attach failure: %s", error_local->message);
g_prefix_error_literal(error, "failed to read firmware: ");
return FALSE;
}
fw = fu_firmware_write(firmware, error);
if (fw == NULL) {
g_autoptr(GError) error_local = NULL;
if (!fu_device_attach_full(device, progress, &error_local))
g_debug("ignoring attach failure: %s", error_local->message);
g_prefix_error_literal(error, "failed to write firmware: ");
return FALSE;
}
for (guint i = 0; checksum_types[i] != 0; i++) {
g_autofree gchar *hash = NULL;
hash = g_compute_checksum_for_bytes(checksum_types[i], fw);
fu_device_add_checksum(device, hash);
}
return fu_device_attach_full(device, progress, error);
}
/**
* fu_plugin_runner_startup:
* @self: a #FuPlugin
* @error: (nullable): optional return location for an error
*
* Runs the startup routine for the plugin
*
* Returns: #TRUE for success, #FALSE for failure
*
* Since: 0.8.0
**/
gboolean
fu_plugin_runner_startup(FuPlugin *self, FuProgress *progress, GError **error)
{
FuPluginVfuncs *vfuncs = fu_plugin_get_vfuncs(self);
g_autoptr(GError) error_local = NULL;
g_return_val_if_fail(FU_IS_PLUGIN(self), FALSE);
/* progress */
fu_progress_set_name(progress, fu_plugin_get_name(self));
/* be helpful for unit tests */
fu_plugin_runner_init(self);
/* not enabled */
if (fu_plugin_has_flag(self, FWUPD_PLUGIN_FLAG_DISABLED))
return TRUE;
/* optional */
if (vfuncs->startup != NULL) {
g_debug("startup(%s)", fu_plugin_get_name(self));
if (!vfuncs->startup(self, progress, &error_local)) {
if (error_local == NULL) {
g_critical("unset plugin error in startup(%s)",
fu_plugin_get_name(self));
g_set_error_literal(&error_local,
FWUPD_ERROR,
FWUPD_ERROR_INTERNAL,
"unspecified error");
/* nocheck:error-false-return */
}
g_propagate_prefixed_error(error,
g_steal_pointer(&error_local),
"failed to startup using %s: ",
fu_plugin_get_name(self));
return FALSE;
}
}
/* success */
return TRUE;
}
/**
* fu_plugin_runner_ready:
* @self: a #FuPlugin
* @error: (nullable): optional return location for an error
*
* Runs the ready routine for the plugin
*
* Returns: #TRUE for success, #FALSE for failure
*
* Since: 1.9.6
**/
gboolean
fu_plugin_runner_ready(FuPlugin *self, FuProgress *progress, GError **error)
{
FuPluginVfuncs *vfuncs = fu_plugin_get_vfuncs(self);
g_autoptr(GError) error_local = NULL;
g_return_val_if_fail(FU_IS_PLUGIN(self), FALSE);
/* progress */
fu_progress_set_name(progress, fu_plugin_get_name(self));
if (fu_plugin_has_flag(self, FWUPD_PLUGIN_FLAG_DISABLED))
return TRUE;
fu_plugin_add_flag(self, FWUPD_PLUGIN_FLAG_READY);
if (vfuncs->ready == NULL)
return TRUE;
/* optional */
g_debug("ready(%s)", fu_plugin_get_name(self));
if (!vfuncs->ready(self, progress, &error_local)) {
if (error_local == NULL) {
g_critical("unset plugin error in ready(%s)", fu_plugin_get_name(self));
g_set_error_literal(&error_local,
FWUPD_ERROR,
FWUPD_ERROR_INTERNAL,
"unspecified error");
/* nocheck:error-false-return */
}
g_propagate_prefixed_error(error,
g_steal_pointer(&error_local),
"failed to ready using %s: ",
fu_plugin_get_name(self));
return FALSE;
}
/* success */
return TRUE;
}
/**
* fu_plugin_runner_init:
* @self: a #FuPlugin
*
* Runs the constructed routine for the plugin, if enabled.
*
* Since: 1.8.1
**/
void
fu_plugin_runner_init(FuPlugin *self)
{
FuPluginVfuncs *vfuncs = fu_plugin_get_vfuncs(self);
FuPluginPrivate *priv = GET_PRIVATE(self);
g_return_if_fail(FU_IS_PLUGIN(self));
/* already done */
if (priv->done_init)
return;
/* not enabled */
if (fu_plugin_has_flag(self, FWUPD_PLUGIN_FLAG_DISABLED))
return;
/* optional */
if (vfuncs->constructed != NULL) {
g_debug("constructed(%s)", fu_plugin_get_name(self));
vfuncs->constructed(G_OBJECT(self));
priv->done_init = TRUE;
}
}
static gboolean
fu_plugin_runner_device_generic(FuPlugin *self,
FuDevice *device,
const gchar *symbol_name,
FuPluginDeviceFunc device_func,
GError **error)
{
g_autoptr(GError) error_local = NULL;
/* not enabled */
if (fu_plugin_has_flag(self, FWUPD_PLUGIN_FLAG_DISABLED))
return TRUE;
/* optional */
if (device_func == NULL)
return TRUE;
g_debug("%s(%s)", symbol_name + 10, fu_plugin_get_name(self));
if (!device_func(self, device, &error_local)) {
if (error_local == NULL) {
g_critical("unset plugin error in %s(%s)",
fu_plugin_get_name(self),
symbol_name + 10);
g_set_error_literal(&error_local,
FWUPD_ERROR,
FWUPD_ERROR_INTERNAL,
"unspecified error");
/* nocheck:error-false-return */
}
g_propagate_prefixed_error(error,
g_steal_pointer(&error_local),
"failed to %s using %s: ",
symbol_name + 10,
fu_plugin_get_name(self));
return FALSE;
}
return TRUE;
}
static gboolean
fu_plugin_runner_device_generic_progress(FuPlugin *self,
FuDevice *device,
FuProgress *progress,
const gchar *symbol_name,
FuPluginDeviceProgressFunc device_func,
GError **error)
{
g_autoptr(GError) error_local = NULL;
/* not enabled */
if (fu_plugin_has_flag(self, FWUPD_PLUGIN_FLAG_DISABLED))
return TRUE;
/* optional */
if (device_func == NULL)
return TRUE;
g_debug("%s(%s)", symbol_name + 10, fu_plugin_get_name(self));
if (!device_func(self, device, progress, &error_local)) {
if (error_local == NULL) {
g_critical("unset plugin error in %s(%s)",
fu_plugin_get_name(self),
symbol_name + 10);
g_set_error_literal(&error_local,
FWUPD_ERROR,
FWUPD_ERROR_INTERNAL,
"unspecified error");
/* nocheck:error-false-return */
}
g_propagate_prefixed_error(error,
g_steal_pointer(&error_local),
"failed to %s using %s: ",
symbol_name + 10,
fu_plugin_get_name(self));
return FALSE;
}
return TRUE;
}
static gboolean
fu_plugin_runner_flagged_device_generic(FuPlugin *self,
FuDevice *device,
FuProgress *progress,
FwupdInstallFlags flags,
const gchar *symbol_name,
FuPluginFlaggedDeviceFunc func,
GError **error)
{
g_autoptr(GError) error_local = NULL;
/* not enabled */
if (fu_plugin_has_flag(self, FWUPD_PLUGIN_FLAG_DISABLED))
return TRUE;
/* optional */
if (func == NULL)
return TRUE;
g_debug("%s(%s)", symbol_name + 10, fu_plugin_get_name(self));
if (!func(self, device, progress, flags, &error_local)) {
if (error_local == NULL) {
g_critical("unset plugin error in %s(%s)",
fu_plugin_get_name(self),
symbol_name + 10);
g_set_error_literal(&error_local,
FWUPD_ERROR,
FWUPD_ERROR_INTERNAL,
"unspecified error");
/* nocheck:error-false-return */
}
g_propagate_prefixed_error(error,
g_steal_pointer(&error_local),
"failed to %s using %s: ",
symbol_name + 10,
fu_plugin_get_name(self));
return FALSE;
}
return TRUE;
}
static gboolean
fu_plugin_runner_device_array_generic(FuPlugin *self,
GPtrArray *devices,
const gchar *symbol_name,
FuPluginDeviceArrayFunc func,
GError **error)
{
g_autoptr(GError) error_local = NULL;
/* not enabled */
if (fu_plugin_has_flag(self, FWUPD_PLUGIN_FLAG_DISABLED))
return TRUE;
/* optional */
if (func == NULL)
return TRUE;
g_debug("%s(%s)", symbol_name + 10, fu_plugin_get_name(self));
if (!func(self, devices, &error_local)) {
if (error_local == NULL) {
g_critical("unset plugin error in for %s(%s)",
fu_plugin_get_name(self),
symbol_name + 10);
g_set_error_literal(&error_local,
FWUPD_ERROR,
FWUPD_ERROR_INTERNAL,
"unspecified error");
/* nocheck:error-false-return */
}
g_propagate_prefixed_error(error,
g_steal_pointer(&error_local),
"failed to %s using %s: ",
symbol_name + 10,
fu_plugin_get_name(self));
return FALSE;
}
return TRUE;
}
/**
* fu_plugin_runner_coldplug:
* @self: a #FuPlugin
* @progress: a #FuProgress
* @error: (nullable): optional return location for an error
*
* Runs the coldplug routine for the plugin
*
* Returns: #TRUE for success, #FALSE for failure
*
* Since: 0.8.0
**/
gboolean
fu_plugin_runner_coldplug(FuPlugin *self, FuProgress *progress, GError **error)
{
FuPluginPrivate *priv = GET_PRIVATE(self);
FuPluginVfuncs *vfuncs = fu_plugin_get_vfuncs(self);
g_autoptr(GError) error_local = NULL;
g_return_val_if_fail(FU_IS_PLUGIN(self), FALSE);
/* progress */
if (fu_plugin_get_name(self) != NULL)
fu_progress_set_name(progress, fu_plugin_get_name(self));
/* not enabled */
if (fu_plugin_has_flag(self, FWUPD_PLUGIN_FLAG_DISABLED))
return TRUE;
/* no HwId */
if (fu_plugin_has_flag(self, FWUPD_PLUGIN_FLAG_REQUIRE_HWID))
return TRUE;
/* optional */
if (vfuncs->coldplug == NULL)
return TRUE;
g_debug("coldplug(%s)", fu_plugin_get_name(self));
if (!vfuncs->coldplug(self, progress, &error_local)) {
if (error_local == NULL) {
g_critical("unset plugin error in coldplug(%s)", fu_plugin_get_name(self));
g_set_error_literal(&error_local,
FWUPD_ERROR,
FWUPD_ERROR_INTERNAL,
"unspecified error");
/* nocheck:error-false-return */
}
/* coldplug failed, but we might have already added devices to the daemon... */
if (priv->devices != NULL) {
for (guint i = 0; i < priv->devices->len; i++) {
FuDevice *device = g_ptr_array_index(priv->devices, i);
g_warning("removing device %s due to failed coldplug",
fu_device_get_id(device));
fu_plugin_device_remove(self, device);
}
}
g_propagate_prefixed_error(error,
g_steal_pointer(&error_local),
"failed to coldplug using %s: ",
fu_plugin_get_name(self));
return FALSE;
}
return TRUE;
}
/**
* fu_plugin_runner_composite_prepare:
* @self: a #FuPlugin
* @devices: (element-type FuDevice): an array of devices
* @error: (nullable): optional return location for an error
*
* Runs the composite_prepare routine for the plugin
*
* Returns: #TRUE for success, #FALSE for failure
*
* Since: 1.0.9
**/
gboolean
fu_plugin_runner_composite_prepare(FuPlugin *self, GPtrArray *devices, GError **error)
{
FuPluginVfuncs *vfuncs = fu_plugin_get_vfuncs(self);
return fu_plugin_runner_device_array_generic(self,
devices,
"fu_plugin_composite_prepare",
vfuncs->composite_prepare,
error);
}
/**
* fu_plugin_runner_composite_cleanup:
* @self: a #FuPlugin
* @devices: (element-type FuDevice): an array of devices
* @error: (nullable): optional return location for an error
*
* Runs the composite_cleanup routine for the plugin
*
* Returns: #TRUE for success, #FALSE for failure
*
* Since: 1.0.9
**/
gboolean
fu_plugin_runner_composite_cleanup(FuPlugin *self, GPtrArray *devices, GError **error)
{
FuPluginVfuncs *vfuncs = fu_plugin_get_vfuncs(self);
return fu_plugin_runner_device_array_generic(self,
devices,
"fu_plugin_composite_cleanup",
vfuncs->composite_cleanup,
error);
}
/**
* fu_plugin_runner_prepare:
* @self: a #FuPlugin
* @device: a device
* @progress: a #FuProgress
* @flags: install flags
* @error: (nullable): optional return location for an error
*
* Runs the update_prepare routine for the plugin
*
* Returns: #TRUE for success, #FALSE for failure
*
* Since: 1.7.0
**/
gboolean
fu_plugin_runner_prepare(FuPlugin *self,
FuDevice *device,
FuProgress *progress,
FwupdInstallFlags flags,
GError **error)
{
FuPluginVfuncs *vfuncs = fu_plugin_get_vfuncs(self);
return fu_plugin_runner_flagged_device_generic(self,
device,
progress,
flags,
"fu_plugin_prepare",
vfuncs->prepare,
error);
}
/**
* fu_plugin_runner_cleanup:
* @self: a #FuPlugin
* @device: a device
* @progress: a #FuProgress
* @flags: install flags
* @error: (nullable): optional return location for an error
*
* Runs the update_cleanup routine for the plugin
*
* Returns: #TRUE for success, #FALSE for failure
*
* Since: 1.7.0
**/
gboolean
fu_plugin_runner_cleanup(FuPlugin *self,
FuDevice *device,
FuProgress *progress,
FwupdInstallFlags flags,
GError **error)
{
FuPluginVfuncs *vfuncs = fu_plugin_get_vfuncs(self);
return fu_plugin_runner_flagged_device_generic(self,
device,
progress,
flags,
"fu_plugin_cleanup",
vfuncs->cleanup,
error);
}
/**
* fu_plugin_runner_attach:
* @self: a #FuPlugin
* @device: a device
* @progress: a #FuProgress
* @error: (nullable): optional return location for an error
*
* Runs the update_attach routine for the plugin
*
* Returns: #TRUE for success, #FALSE for failure
*
* Since: 1.7.0
**/
gboolean
fu_plugin_runner_attach(FuPlugin *self, FuDevice *device, FuProgress *progress, GError **error)
{
FuPluginVfuncs *vfuncs = fu_plugin_get_vfuncs(self);
return fu_plugin_runner_device_generic_progress(
self,
device,
progress,
"fu_plugin_attach",
vfuncs->attach != NULL ? vfuncs->attach : fu_plugin_device_attach,
error);
}
/**
* fu_plugin_runner_detach:
* @self: a #FuPlugin
* @device: a device
* @progress: a #FuProgress
* @error: (nullable): optional return location for an error
*
* Runs the update_detach routine for the plugin
*
* Returns: #TRUE for success, #FALSE for failure
*
* Since: 1.7.0
**/
gboolean
fu_plugin_runner_detach(FuPlugin *self, FuDevice *device, FuProgress *progress, GError **error)
{
FuPluginVfuncs *vfuncs = fu_plugin_get_vfuncs(self);
return fu_plugin_runner_device_generic_progress(
self,
device,
progress,
"fu_plugin_detach",
vfuncs->detach != NULL ? vfuncs->detach : fu_plugin_device_detach,
error);
}
/**
* fu_plugin_runner_reload:
* @self: a #FuPlugin
* @device: a device
* @error: (nullable): optional return location for an error
*
* Runs reload routine for a device
*
* Returns: #TRUE for success, #FALSE for failure
*
* Since: 1.7.0
**/
gboolean
fu_plugin_runner_reload(FuPlugin *self, FuDevice *device, GError **error)
{
FuDevice *proxy = fu_device_get_proxy_with_fallback(device);
g_autoptr(FuDeviceLocker) locker = NULL;
/* not enabled */
if (fu_plugin_has_flag(self, FWUPD_PLUGIN_FLAG_DISABLED))
return TRUE;
/* no object loaded */
locker = fu_device_locker_new(proxy, error);
if (locker == NULL)
return FALSE;
return fu_device_reload(device, error);
}
/**
* fu_plugin_runner_reboot_cleanup:
* @self: a #FuPlugin
* @device: a device
* @error: (nullable): optional return location for an error
*
* Performs cleanup actions after the reboot has been performed.
*
* Returns: #TRUE for success, #FALSE for failure
*
* Since: 1.9.7
**/
gboolean
fu_plugin_runner_reboot_cleanup(FuPlugin *self, FuDevice *device, GError **error)
{
FuPluginVfuncs *vfuncs = fu_plugin_get_vfuncs(self);
/* optional */
if (fu_plugin_has_flag(self, FWUPD_PLUGIN_FLAG_DISABLED))
return TRUE;
if (vfuncs->reboot_cleanup == NULL)
return TRUE;
g_debug("reboot_cleanup(%s)", fu_plugin_get_name(self));
return vfuncs->reboot_cleanup(self, device, error);
}
/**
* fu_plugin_runner_add_security_attrs:
* @self: a #FuPlugin
* @attrs: a security attribute
*
* Runs the `add_security_attrs()` routine for the plugin
*
* Since: 1.5.0
**/
void
fu_plugin_runner_add_security_attrs(FuPlugin *self, FuSecurityAttrs *attrs)
{
FuPluginVfuncs *vfuncs = fu_plugin_get_vfuncs(self);
/* optional, but gets called even for disabled plugins */
if (vfuncs->add_security_attrs == NULL)
return;
g_debug("add_security_attrs(%s)", fu_plugin_get_name(self));
vfuncs->add_security_attrs(self, attrs);
}
/**
* fu_plugin_add_device_gtype:
* @self: a #FuPlugin
* @device_gtype: a #GType, e.g. `FU_TYPE_DEVICE`
*
* Adds the device #GType which is used when creating devices.
*
* If this method is used then fu_plugin_backend_device_added() is not called, and
* instead the object is created in the daemon for the plugin.
*
* Plugins can use this method only in fu_plugin_init()
*
* See also: fu_plugin_set_device_gtype_default()
*
* Since: 1.6.0
**/
void
fu_plugin_add_device_gtype(FuPlugin *self, GType device_gtype)
{
FuPluginPrivate *priv = GET_PRIVATE(self);
g_return_if_fail(FU_IS_PLUGIN(self));
/* create as required */
if (priv->device_gtypes == NULL)
priv->device_gtypes = g_array_new(FALSE, FALSE, sizeof(GType));
/* check for duplicates */
for (guint i = 0; i < priv->device_gtypes->len; i++) {
GType device_gtype_tmp = g_array_index(priv->device_gtypes, GType, i);
if (device_gtype == device_gtype_tmp)
return;
}
/* ensure (to allow quirks to use it) then add */
g_type_ensure(device_gtype);
g_array_append_val(priv->device_gtypes, device_gtype);
}
/**
* fu_plugin_get_device_gtypes:
* @self: a #FuPlugin
*
* Gets all device #GTypes.
*
* Returns: (element-type GType) (transfer none) (nullable): registered types, or %NULL
*
* Since: 2.0.2
**/
GArray *
fu_plugin_get_device_gtypes(FuPlugin *self)
{
FuPluginPrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FU_IS_PLUGIN(self), NULL);
return priv->device_gtypes;
}
/**
* fu_plugin_get_device_gtype_default:
* @self: a #FuPlugin
*
* Gets the default device #GType.
*
* If there is only one possible #GType added from fu_plugin_add_device_gtype() it will also be
* returned here.
*
* Returns: a #GType, or %G_TYPE_INVALID on error
*
* Since: 1.9.14
**/
GType
fu_plugin_get_device_gtype_default(FuPlugin *self)
{
FuPluginPrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FU_IS_PLUGIN(self), G_TYPE_INVALID);
if (priv->device_gtype_default != G_TYPE_INVALID)
return priv->device_gtype_default;
if (priv->device_gtypes->len == 1)
return g_array_index(priv->device_gtypes, GType, 0);
return G_TYPE_INVALID;
}
/**
* fu_plugin_set_device_gtype_default:
* @self: a #FuPlugin
* @device_gtype: a #GType, e.g. `FU_TYPE_DEVICE`
*
* Sets the default device #GType.
*
* This will also add the device #GType using fu_plugin_add_device_gtype().
*
* Plugins can use this method only in fu_plugin_init()
*
* Since: 1.9.14
**/
void
fu_plugin_set_device_gtype_default(FuPlugin *self, GType device_gtype)
{
FuPluginPrivate *priv = GET_PRIVATE(self);
g_return_if_fail(FU_IS_PLUGIN(self));
fu_plugin_add_device_gtype(self, device_gtype);
priv->device_gtype_default = device_gtype;
}
static gchar *
fu_plugin_string_uncamelcase(const gchar *str)
{
GString *tmp = g_string_new(NULL);
for (guint i = 0; str[i] != '\0'; i++) {
if (g_ascii_islower(str[i]) || g_ascii_isdigit(str[i])) {
g_string_append_c(tmp, str[i]);
continue;
}
if (i > 0)
g_string_append_c(tmp, '-');
g_string_append_c(tmp, g_ascii_tolower(str[i]));
}
return g_string_free(tmp, FALSE);
}
static gboolean
fu_plugin_check_amdgpu_dpaux(FuPlugin *self, GError **error)
{
#ifdef __linux__
gsize bufsz = 0;
g_autofree gchar *buf = NULL;
g_auto(GStrv) lines = NULL;
/* no module support in the kernel, we can't test for amdgpu module */
if (!g_file_test("/proc/modules", G_FILE_TEST_EXISTS))
return TRUE;
if (!g_file_get_contents("/proc/modules", &buf, &bufsz, error))
return FALSE;
lines = g_strsplit(buf, "\n", -1);
for (guint i = 0; lines[i] != NULL; i++) {
if (g_str_has_prefix(lines[i], "amdgpu ")) {
/* released 2019! */
return fu_kernel_check_version("5.2.0", error);
}
}
#endif
return TRUE;
}
/**
* fu_plugin_add_device_udev_subsystem:
* @self: a #FuPlugin
* @subsystem: a subsystem name, e.g. `pciport`
*
* Add this plugin as a possible handler of devices with this udev subsystem.
* Use fu_plugin_add_udev_subsystem() if you just want to ensure the subsystem is watched.
*
* Plugins can use this method only in fu_plugin_init()
*
* Since: 1.9.3
**/
void
fu_plugin_add_device_udev_subsystem(FuPlugin *self, const gchar *subsystem)
{
FuPluginPrivate *priv = GET_PRIVATE(self);
g_return_if_fail(FU_IS_PLUGIN(self));
g_return_if_fail(subsystem != NULL);
/* see https://github.com/fwupd/fwupd/issues/1121 for more details */
if (g_strcmp0(subsystem, "drm_dp_aux_dev") == 0) {
g_autoptr(GError) error = NULL;
if (!fu_plugin_check_amdgpu_dpaux(self, &error)) {
g_warning("failed to add subsystem: %s", error->message);
fu_plugin_add_flag(self, FWUPD_PLUGIN_FLAG_DISABLED);
fu_plugin_add_flag(self, FWUPD_PLUGIN_FLAG_KERNEL_TOO_OLD);
return;
}
}
/* proxy */
fu_context_add_udev_subsystem(priv->ctx, subsystem, fu_plugin_get_name(self));
}
/**
* fu_plugin_add_udev_subsystem:
* @self: a #FuPlugin
* @subsystem: a subsystem name, e.g. `pciport`
*
* Registers the udev subsystem to be watched by the daemon.
*
* Plugins can use this method only in fu_plugin_init()
*
* Since: 1.6.2
**/
void
fu_plugin_add_udev_subsystem(FuPlugin *self, const gchar *subsystem)
{
FuPluginPrivate *priv = GET_PRIVATE(self);
g_return_if_fail(FU_IS_PLUGIN(self));
g_return_if_fail(subsystem != NULL);
/* proxy */
fu_context_add_udev_subsystem(priv->ctx, subsystem, NULL);
}
/**
* fu_plugin_add_firmware_gtype:
* @self: a #FuPlugin
* @id: (nullable): an optional string describing the type, e.g. `ihex`
* @gtype: a #GType e.g. `FU_TYPE_FOO_FIRMWARE`
*
* Adds a firmware #GType which is used when creating devices. If @id is not
* specified then it is guessed using the #GType name.
*
* Plugins can use this method only in fu_plugin_init()
*
* Since: 1.3.3
**/
void
fu_plugin_add_firmware_gtype(FuPlugin *self, const gchar *id, GType gtype)
{
FuPluginPrivate *priv = GET_PRIVATE(self);
g_autofree gchar *id_safe = NULL;
if (id != NULL) {
id_safe = g_strdup(id);
} else {
g_autoptr(GString) str = g_string_new(g_type_name(gtype));
if (g_str_has_prefix(str->str, "Fu"))
g_string_erase(str, 0, 2);
g_string_replace(str, "Firmware", "", 1);
id_safe = fu_plugin_string_uncamelcase(str->str);
}
fu_context_add_firmware_gtype(priv->ctx, id_safe, gtype);
}
static gboolean
fu_plugin_check_supported_device(FuPlugin *self, FuDevice *device)
{
GPtrArray *instance_ids = fu_device_get_instance_ids(device);
for (guint i = 0; i < instance_ids->len; i++) {
const gchar *instance_id = g_ptr_array_index(instance_ids, i);
g_autofree gchar *guid = fwupd_guid_hash_string(instance_id);
if (fu_plugin_check_supported(self, guid))
return TRUE;
}
return FALSE;
}
static gboolean
fu_plugin_backend_device_added(FuPlugin *self,
FuDevice *device,
FuProgress *progress,
GError **error)
{
FuDevice *proxy;
FuPluginPrivate *priv = GET_PRIVATE(self);
GType device_gtype = fu_device_get_specialized_gtype(FU_DEVICE(device));
GType proxy_gtype = fu_device_get_proxy_gtype(FU_DEVICE(device));
g_autoptr(FuDevice) dev = NULL;
g_autoptr(FuDeviceLocker) locker = NULL;
/* progress */
fu_progress_set_id(progress, G_STRLOC);
fu_progress_add_flag(progress, FU_PROGRESS_FLAG_NO_PROFILE);
fu_progress_add_step(progress, FWUPD_STATUS_LOADING, 4, "created");
fu_progress_add_step(progress, FWUPD_STATUS_LOADING, 48, "open");
fu_progress_add_step(progress, FWUPD_STATUS_LOADING, 48, "add");
/* fall back to plugin default */
if (device_gtype == G_TYPE_INVALID)
device_gtype = fu_plugin_get_device_gtype_default(self);
if (device_gtype == G_TYPE_INVALID) {
if (priv->device_gtypes->len > 1) {
g_autoptr(GString) str = g_string_new(NULL);
for (guint i = 0; i < priv->device_gtypes->len; i++) {
device_gtype = g_array_index(priv->device_gtypes, GType, i);
if (str->len > 0)
g_string_append(str, ",");
g_string_append(str, g_type_name(device_gtype));
}
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_INTERNAL,
"too many GTypes to choose a default, got: %s",
str->str);
return FALSE;
}
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_INTERNAL,
"no possible device GTypes");
return FALSE;
}
/* create new device and incorporate existing properties */
dev = g_object_new(device_gtype, "context", priv->ctx, NULL);
fu_device_incorporate(dev, FU_DEVICE(device), FU_DEVICE_INCORPORATE_FLAG_ALL);
/* any proxy device to create too? */
if (proxy_gtype != G_TYPE_INVALID) {
g_autoptr(FuDevice) proxy_tmp =
g_object_new(proxy_gtype, "context", priv->ctx, NULL);
fu_device_incorporate(proxy_tmp, device, FU_DEVICE_INCORPORATE_FLAG_ALL);
fu_device_add_private_flag(dev, FU_DEVICE_PRIVATE_FLAG_REFCOUNTED_PROXY);
fu_device_set_proxy(dev, proxy_tmp);
}
/* notify plugins */
if (!fu_plugin_runner_device_created(self, dev, error))
return FALSE;
fu_progress_step_done(progress);
/* there are a lot of different devices that match, but not all respond
* well to opening -- so limit some ones with issued updates */
if (fu_device_has_private_flag(dev, FU_DEVICE_PRIVATE_FLAG_ONLY_SUPPORTED)) {
if (!fu_device_probe(dev, error))
return FALSE;
fu_device_convert_instance_ids(dev);
if (!fu_plugin_check_supported_device(self, dev)) {
GPtrArray *guids = fu_device_get_guids(dev);
g_autofree gchar *guids_str = fu_strjoin(",", guids);
g_info("%s has no updates, so ignoring device", guids_str);
fu_progress_finished(progress);
return TRUE;
}
}
/* open */
proxy = fu_device_get_proxy_internal(dev);
if (proxy != NULL) {
g_autoptr(FuDeviceLocker) locker_proxy = NULL;
locker_proxy = fu_device_locker_new(proxy, error);
if (locker_proxy == NULL)
return FALSE;
fu_device_incorporate(dev, proxy, FU_DEVICE_INCORPORATE_FLAG_ALL);
}
locker = fu_device_locker_new(dev, error);
if (locker == NULL)
return FALSE;
fu_progress_step_done(progress);
/* add */
fu_plugin_device_add(self, dev);
fu_plugin_runner_device_added(self, dev);
fu_progress_step_done(progress);
return TRUE;
}
/**
* fu_plugin_runner_backend_device_added:
* @self: a #FuPlugin
* @device: a device
* @progress: a #FuProgress
* @error: (nullable): optional return location for an error
*
* Call the backend_device_added routine for the plugin
*
* Returns: #TRUE for success, #FALSE for failure
*
* Since: 1.5.6
**/
gboolean
fu_plugin_runner_backend_device_added(FuPlugin *self,
FuDevice *device,
FuProgress *progress,
GError **error)
{
FuPluginPrivate *priv = GET_PRIVATE(self);
FuPluginVfuncs *vfuncs = fu_plugin_get_vfuncs(self);
g_autoptr(GError) error_local = NULL;
g_return_val_if_fail(FU_IS_PLUGIN(self), FALSE);
g_return_val_if_fail(FU_IS_DEVICE(device), FALSE);
g_return_val_if_fail(FU_IS_PROGRESS(progress), FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
/* not enabled */
if (fu_plugin_has_flag(self, FWUPD_PLUGIN_FLAG_DISABLED))
return TRUE;
/* optional */
if (vfuncs->backend_device_added == NULL) {
if (priv->device_gtypes != NULL ||
fu_device_get_specialized_gtype(device) != G_TYPE_INVALID) {
return fu_plugin_backend_device_added(self, device, progress, error);
}
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_INTERNAL,
"No device GType set");
return FALSE;
}
g_debug("backend_device_added(%s)", fu_plugin_get_name(self));
if (!vfuncs->backend_device_added(self, device, progress, &error_local)) {
if (error_local == NULL) {
g_critical("unset plugin error in backend_device_added(%s)",
fu_plugin_get_name(self));
g_set_error_literal(&error_local,
FWUPD_ERROR,
FWUPD_ERROR_INTERNAL,
"unspecified error");
/* nocheck:error-false-return */
}
g_propagate_prefixed_error(error,
g_steal_pointer(&error_local),
"failed to add device using on %s: ",
fu_plugin_get_name(self));
return FALSE;
}
return TRUE;
}
/**
* fu_plugin_runner_backend_device_changed:
* @self: a #FuPlugin
* @device: a device
* @error: (nullable): optional return location for an error
*
* Call the backend_device_changed routine for the plugin
*
* Returns: #TRUE for success, #FALSE for failure
*
* Since: 1.5.6
**/
gboolean
fu_plugin_runner_backend_device_changed(FuPlugin *self, FuDevice *device, GError **error)
{
FuPluginVfuncs *vfuncs = fu_plugin_get_vfuncs(self);
g_autoptr(GError) error_local = NULL;
g_return_val_if_fail(FU_IS_PLUGIN(self), FALSE);
g_return_val_if_fail(FU_IS_DEVICE(device), FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
/* not enabled */
if (fu_plugin_has_flag(self, FWUPD_PLUGIN_FLAG_DISABLED))
return TRUE;
/* optional */
if (vfuncs->backend_device_changed == NULL)
return TRUE;
g_debug("udev_device_changed(%s)", fu_plugin_get_name(self));
if (!vfuncs->backend_device_changed(self, device, &error_local)) {
if (error_local == NULL) {
g_critical("unset plugin error in udev_device_changed(%s)",
fu_plugin_get_name(self));
g_set_error_literal(&error_local,
FWUPD_ERROR,
FWUPD_ERROR_INTERNAL,
"unspecified error");
/* nocheck:error-false-return */
}
g_propagate_prefixed_error(error,
g_steal_pointer(&error_local),
"failed to change device on %s: ",
fu_plugin_get_name(self));
return FALSE;
}
return TRUE;
}
/**
* fu_plugin_runner_device_added:
* @self: a #FuPlugin
* @device: a device
*
* Call the device_added routine for the plugin
*
* Since: 1.5.0
**/
void
fu_plugin_runner_device_added(FuPlugin *self, FuDevice *device)
{
FuPluginVfuncs *vfuncs = fu_plugin_get_vfuncs(self);
/* not enabled */
if (fu_plugin_has_flag(self, FWUPD_PLUGIN_FLAG_DISABLED))
return;
/* optional */
if (vfuncs->device_added == NULL)
return;
g_debug("fu_plugin_device_added(%s)", fu_plugin_get_name(self));
vfuncs->device_added(self, device);
}
/**
* fu_plugin_runner_device_removed:
* @self: a #FuPlugin
* @device: a device
*
* Call the device_removed routine for the plugin
*
* Since: 1.1.2
**/
void
fu_plugin_runner_device_removed(FuPlugin *self, FuDevice *device)
{
FuPluginVfuncs *vfuncs = fu_plugin_get_vfuncs(self);
g_autoptr(GError) error_local = NULL;
if (!fu_plugin_runner_device_generic(self,
device,
"fu_plugin_backend_device_removed",
vfuncs->backend_device_removed,
&error_local))
g_warning("%s", error_local->message);
}
/**
* fu_plugin_runner_device_register:
* @self: a #FuPlugin
* @device: a device
*
* Call the device_registered routine for the plugin
*
* Since: 0.9.7
**/
void
fu_plugin_runner_device_register(FuPlugin *self, FuDevice *device)
{
FuPluginVfuncs *vfuncs = fu_plugin_get_vfuncs(self);
/* not enabled */
if (fu_plugin_has_flag(self, FWUPD_PLUGIN_FLAG_DISABLED))
return;
/* optional */
if (vfuncs->device_registered != NULL) {
g_debug("fu_plugin_device_registered(%s)", fu_plugin_get_name(self));
vfuncs->device_registered(self, device);
}
}
/**
* fu_plugin_runner_device_created:
* @self: a #FuPlugin
* @device: a device
* @error: (nullable): optional return location for an error
*
* Call the device_created routine for the plugin
*
* Returns: #TRUE for success, #FALSE for failure
*
* Since: 1.4.0
**/
gboolean
fu_plugin_runner_device_created(FuPlugin *self, FuDevice *device, GError **error)
{
FuPluginVfuncs *vfuncs = fu_plugin_get_vfuncs(self);
g_return_val_if_fail(FU_IS_PLUGIN(self), FALSE);
g_return_val_if_fail(FU_IS_DEVICE(device), FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
/* not enabled */
if (fu_plugin_has_flag(self, FWUPD_PLUGIN_FLAG_DISABLED))
return TRUE;
/* optional */
if (vfuncs->device_created == NULL)
return TRUE;
g_debug("fu_plugin_device_created(%s)", fu_plugin_get_name(self));
return vfuncs->device_created(self, device, error);
}
/**
* fu_plugin_runner_verify:
* @self: a #FuPlugin
* @device: a device
* @progress: a #FuProgress
* @flags: verify flags
* @error: (nullable): optional return location for an error
*
* Call into the plugin's verify routine
*
* Returns: #TRUE for success, #FALSE for failure
*
* Since: 0.8.0
**/
gboolean
fu_plugin_runner_verify(FuPlugin *self,
FuDevice *device,
FuProgress *progress,
FuPluginVerifyFlags flags,
GError **error)
{
FuPluginVfuncs *vfuncs = fu_plugin_get_vfuncs(self);
GPtrArray *checksums;
g_autoptr(GError) error_local = NULL;
g_return_val_if_fail(FU_IS_PLUGIN(self), FALSE);
g_return_val_if_fail(FU_IS_DEVICE(device), FALSE);
g_return_val_if_fail(FU_IS_PROGRESS(progress), FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
/* not enabled */
if (fu_plugin_has_flag(self, FWUPD_PLUGIN_FLAG_DISABLED))
return TRUE;
/* optional */
if (vfuncs->verify == NULL) {
if (!fu_device_has_flag(device, FWUPD_DEVICE_FLAG_CAN_VERIFY)) {
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_NOT_SUPPORTED,
"device %s does not support verification",
fu_device_get_id(device));
return FALSE;
}
return fu_plugin_device_read_firmware(self, device, progress, error);
}
/* clear any existing verification checksums */
checksums = fu_device_get_checksums(device);
g_ptr_array_set_size(checksums, 0);
/* run additional detach */
if (!fu_plugin_runner_device_generic_progress(
self,
device,
progress,
"fu_plugin_detach",
vfuncs->detach != NULL ? vfuncs->detach : fu_plugin_device_detach,
error))
return FALSE;
/* run vfunc */
g_debug("verify(%s)", fu_plugin_get_name(self));
if (!vfuncs->verify(self, device, progress, flags, &error_local)) {
g_autoptr(GError) error_attach = NULL;
if (error_local == NULL) {
g_critical("unset plugin error in verify(%s)", fu_plugin_get_name(self));
g_set_error_literal(&error_local,
FWUPD_ERROR,
FWUPD_ERROR_INTERNAL,
"unspecified error");
/* nocheck:error-false-return */
}
g_propagate_prefixed_error(error,
g_steal_pointer(&error_local),
"failed to verify using %s: ",
fu_plugin_get_name(self));
/* make the device "work" again, but don't prefix the error */
if (!fu_plugin_runner_device_generic_progress(
self,
device,
progress,
"fu_plugin_attach",
vfuncs->attach != NULL ? vfuncs->attach : fu_plugin_device_attach,
&error_attach)) {
g_warning("failed to attach whilst aborting verify(): %s",
error_attach->message);
}
return FALSE;
}
/* run optional attach */
if (!fu_plugin_runner_device_generic_progress(
self,
device,
progress,
"fu_plugin_attach",
vfuncs->attach != NULL ? vfuncs->attach : fu_plugin_device_attach,
error))
return FALSE;
/* success */
return TRUE;
}
/**
* fu_plugin_runner_activate:
* @self: a #FuPlugin
* @device: a device
* @progress: a #FuProgress
* @error: (nullable): optional return location for an error
*
* Call into the plugin's activate routine
*
* Returns: #TRUE for success, #FALSE for failure
*
* Since: 1.2.6
**/
gboolean
fu_plugin_runner_activate(FuPlugin *self, FuDevice *device, FuProgress *progress, GError **error)
{
FuPluginVfuncs *vfuncs = fu_plugin_get_vfuncs(self);
guint64 flags;
g_return_val_if_fail(FU_IS_PLUGIN(self), FALSE);
g_return_val_if_fail(FU_IS_DEVICE(device), FALSE);
g_return_val_if_fail(FU_IS_PROGRESS(progress), FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
/* final check */
flags = fu_device_get_flags(device);
if ((flags & FWUPD_DEVICE_FLAG_NEEDS_ACTIVATION) == 0) {
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_NOT_SUPPORTED,
"Device %s does not need activation",
fu_device_get_id(device));
return FALSE;
}
/* run vfunc */
if (!fu_plugin_runner_device_generic_progress(
self,
device,
progress,
"fu_plugin_activate",
vfuncs->activate != NULL ? vfuncs->activate : fu_plugin_device_activate,
error))
return FALSE;
/* update with correct flags */
fu_device_remove_flag(device, FWUPD_DEVICE_FLAG_NEEDS_ACTIVATION);
fu_device_set_modified_usec(device, g_get_real_time());
return TRUE;
}
/**
* fu_plugin_runner_unlock:
* @self: a #FuPlugin
* @device: a device
* @error: (nullable): optional return location for an error
*
* Call into the plugin's unlock routine
*
* Returns: #TRUE for success, #FALSE for failure
*
* Since: 0.8.0
**/
gboolean
fu_plugin_runner_unlock(FuPlugin *self, FuDevice *device, GError **error)
{
FuPluginVfuncs *vfuncs = fu_plugin_get_vfuncs(self);
guint64 flags;
g_return_val_if_fail(FU_IS_PLUGIN(self), FALSE);
g_return_val_if_fail(FU_IS_DEVICE(device), FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
/* final check */
flags = fu_device_get_flags(device);
if ((flags & FWUPD_DEVICE_FLAG_LOCKED) == 0) {
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_NOT_SUPPORTED,
"Device %s is not locked",
fu_device_get_id(device));
return FALSE;
}
/* run vfunc */
if (!fu_plugin_runner_device_generic(self,
device,
"fu_plugin_unlock",
vfuncs->unlock,
error))
return FALSE;
/* update with correct flags */
fu_device_remove_flag(device, FWUPD_DEVICE_FLAG_LOCKED);
fu_device_set_modified_usec(device, g_get_real_time());
return TRUE;
}
/**
* fu_plugin_runner_write_firmware:
* @self: a #FuPlugin
* @device: a device
* @firmware: a #FuFirmware
* @progress: a #FuProgress
* @flags: install flags
* @error: (nullable): optional return location for an error
*
* Call into the plugin's write firmware routine
*
* Returns: #TRUE for success, #FALSE for failure
*
* Since: 2.0.7
**/
gboolean
fu_plugin_runner_write_firmware(FuPlugin *self,
FuDevice *device,
FuFirmware *firmware,
FuProgress *progress,
FwupdInstallFlags flags,
GError **error)
{
FuPluginVfuncs *vfuncs = fu_plugin_get_vfuncs(self);
g_autoptr(GError) error_local = NULL;
g_return_val_if_fail(FU_IS_PLUGIN(self), FALSE);
g_return_val_if_fail(FU_IS_DEVICE(device), FALSE);
g_return_val_if_fail(FU_IS_FIRMWARE(firmware), FALSE);
g_return_val_if_fail(FU_IS_PROGRESS(progress), FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
/* not enabled */
if (fu_plugin_has_flag(self, FWUPD_PLUGIN_FLAG_DISABLED)) {
g_debug("plugin not enabled, skipping");
return TRUE;
}
/* optional */
if (vfuncs->write_firmware != NULL) {
if (!vfuncs
->write_firmware(self, device, firmware, progress, flags, &error_local)) {
if (error_local == NULL) {
g_critical("unset plugin error in update(%s)",
fu_plugin_get_name(self));
g_set_error_literal(&error_local,
FWUPD_ERROR,
FWUPD_ERROR_INTERNAL,
"unspecified error");
return FALSE;
}
fu_device_set_update_error(device, error_local->message);
g_propagate_error(error, g_steal_pointer(&error_local));
return FALSE;
}
} else {
g_debug("superclassed write_firmware(%s)", fu_plugin_get_name(self));
return fu_plugin_device_write_firmware(self,
device,
firmware,
progress,
flags,
error);
}
/* no longer valid */
if (!fu_device_has_flag(device, FWUPD_DEVICE_FLAG_NEEDS_REBOOT) &&
!fu_device_has_flag(device, FWUPD_DEVICE_FLAG_NEEDS_SHUTDOWN)) {
GPtrArray *checksums = fu_device_get_checksums(device);
g_ptr_array_set_size(checksums, 0);
}
/* success */
return TRUE;
}
/**
* fu_plugin_runner_composite_peek_firmware:
* @self: a #FuPlugin
* @device: a device
* @firmware: a #FuFirmware
* @progress: a #FuProgress
* @flags: install flags
* @error: (nullable): optional return location for an error
*
* Notify when the firmware has been parsed and the update is ready to be deployed
*
* Returns: #TRUE for success, #FALSE for failure
*
* Since: 2.0.19
**/
gboolean
fu_plugin_runner_composite_peek_firmware(FuPlugin *self,
FuDevice *device,
FuFirmware *firmware,
FuProgress *progress,
FwupdInstallFlags flags,
GError **error)
{
FuPluginVfuncs *vfuncs = fu_plugin_get_vfuncs(self);
g_autoptr(GError) error_local = NULL;
g_return_val_if_fail(FU_IS_PLUGIN(self), FALSE);
g_return_val_if_fail(FU_IS_DEVICE(device), FALSE);
g_return_val_if_fail(FU_IS_FIRMWARE(firmware), FALSE);
g_return_val_if_fail(FU_IS_PROGRESS(progress), FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
/* not enabled */
if (fu_plugin_has_flag(self, FWUPD_PLUGIN_FLAG_DISABLED)) {
g_debug("plugin not enabled, skipping");
return TRUE;
}
/* optional */
if (vfuncs->composite_peek_firmware == NULL)
return TRUE;
if (!vfuncs
->composite_peek_firmware(self, device, firmware, progress, flags, &error_local)) {
if (error_local == NULL) {
g_critical("unset plugin error in update(%s)", fu_plugin_get_name(self));
g_set_error_literal(&error_local,
FWUPD_ERROR,
FWUPD_ERROR_INTERNAL,
"unspecified error");
return FALSE;
}
fu_device_set_update_error(device, error_local->message);
g_propagate_error(error, g_steal_pointer(&error_local));
return FALSE;
}
/* success */
return TRUE;
}
/**
* fu_plugin_runner_clear_results:
* @self: a #FuPlugin
* @device: a device
* @error: (nullable): optional return location for an error
*
* Call into the plugin's clear results routine
*
* Returns: #TRUE for success, #FALSE for failure
*
* Since: 0.8.0
**/
gboolean
fu_plugin_runner_clear_results(FuPlugin *self, FuDevice *device, GError **error)
{
FuPluginVfuncs *vfuncs = fu_plugin_get_vfuncs(self);
g_autoptr(GError) error_local = NULL;
g_return_val_if_fail(FU_IS_PLUGIN(self), FALSE);
g_return_val_if_fail(FU_IS_DEVICE(device), FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
/* not enabled */
if (fu_plugin_has_flag(self, FWUPD_PLUGIN_FLAG_DISABLED))
return TRUE;
/* optional */
if (vfuncs->clear_results == NULL)
return TRUE;
g_debug("clear_result(%s)", fu_plugin_get_name(self));
if (!vfuncs->clear_results(self, device, &error_local)) {
if (error_local == NULL) {
g_critical("unset plugin error in clear_result(%s)",
fu_plugin_get_name(self));
g_set_error_literal(&error_local,
FWUPD_ERROR,
FWUPD_ERROR_INTERNAL,
"unspecified error");
/* nocheck:error-false-return */
}
g_propagate_prefixed_error(error,
g_steal_pointer(&error_local),
"failed to clear_result using %s: ",
fu_plugin_get_name(self));
return FALSE;
}
return TRUE;
}
/**
* fu_plugin_runner_get_results:
* @self: a #FuPlugin
* @device: a device
* @error: (nullable): optional return location for an error
*
* Call into the plugin's get results routine
*
* Returns: #TRUE for success, #FALSE for failure
*
* Since: 0.8.0
**/
gboolean
fu_plugin_runner_get_results(FuPlugin *self, FuDevice *device, GError **error)
{
FuPluginVfuncs *vfuncs = fu_plugin_get_vfuncs(self);
g_autoptr(GError) error_local = NULL;
g_return_val_if_fail(FU_IS_PLUGIN(self), FALSE);
g_return_val_if_fail(FU_IS_DEVICE(device), FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
/* not enabled */
if (fu_plugin_has_flag(self, FWUPD_PLUGIN_FLAG_DISABLED))
return TRUE;
/* optional */
if (vfuncs->get_results == NULL) {
g_debug("superclassed get_results(%s)", fu_plugin_get_name(self));
return fu_plugin_device_get_results(self, device, error);
}
g_debug("get_results(%s)", fu_plugin_get_name(self));
if (!vfuncs->get_results(self, device, &error_local)) {
if (error_local == NULL) {
g_critical("unset plugin error in get_results(%s)",
fu_plugin_get_name(self));
g_set_error_literal(&error_local,
FWUPD_ERROR,
FWUPD_ERROR_INTERNAL,
"unspecified error");
/* nocheck:error-false-return */
}
g_propagate_prefixed_error(error,
g_steal_pointer(&error_local),
"failed to get_results using %s: ",
fu_plugin_get_name(self));
return FALSE;
}
return TRUE;
}
/**
* fu_plugin_runner_fix_host_security_attr:
* @self: a #FuPlugin
* @attr: (nullable): a security attribute
* @error: (nullable): optional return location for an error
*
* Fix the specific security attribute.
*
* Returns: #TRUE for success, #FALSE for failure
*
* Since: 1.9.6
**/
gboolean
fu_plugin_runner_fix_host_security_attr(FuPlugin *self, FwupdSecurityAttr *attr, GError **error)
{
FuPluginVfuncs *vfuncs = fu_plugin_get_vfuncs(self);
g_return_val_if_fail(FU_IS_PLUGIN(self), FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
if (vfuncs->fix_host_security_attr == NULL) {
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_NOT_SUPPORTED,
"fix is not supported");
return FALSE;
}
return vfuncs->fix_host_security_attr(self, attr, error);
}
/**
* fu_plugin_runner_undo_host_security_attr:
* @self: a #FuPlugin
* @attr: (nullable): a security attribute
* @error: (nullable): optional return location for an error
*
* Fix the security issue of given security attribute.
*
* Returns: #TRUE for success, #FALSE for failure
*
* Since: 1.9.6
**/
gboolean
fu_plugin_runner_undo_host_security_attr(FuPlugin *self, FwupdSecurityAttr *attr, GError **error)
{
FuPluginVfuncs *vfuncs = fu_plugin_get_vfuncs(self);
g_return_val_if_fail(FU_IS_PLUGIN(self), FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
if (vfuncs->undo_host_security_attr == NULL) {
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_NOT_SUPPORTED,
"undo is not supported");
return FALSE;
}
return vfuncs->undo_host_security_attr(self, attr, error);
}
/**
* fu_plugin_runner_modify_config:
* @self: a #FuPlugin
* @key: a config key
* @value: a config value
* @error: (nullable): optional return location for an error
*
* Sets a plugin config option, which may be allow-listed or value-checked.
*
* Returns: #TRUE for success, #FALSE for failure
*
* Since: 2.0.0
**/
gboolean
fu_plugin_runner_modify_config(FuPlugin *self, const gchar *key, const gchar *value, GError **error)
{
FuPluginVfuncs *vfuncs = fu_plugin_get_vfuncs(self);
/* optional */
if (vfuncs->modify_config == NULL) {
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_NOT_SUPPORTED,
"cannot modify %s=%s config",
key,
value);
return FALSE;
}
g_debug("modify_config(%s)", fu_plugin_get_name(self));
return vfuncs->modify_config(self, key, value, error);
}
/**
* fu_plugin_get_order:
* @self: a #FuPlugin
*
* Gets the plugin order, where higher numbers are run after lower
* numbers.
*
* Returns: the integer value
*
* Since: 1.0.0
**/
guint
fu_plugin_get_order(FuPlugin *self)
{
FuPluginPrivate *priv = fu_plugin_get_instance_private(self);
return priv->order;
}
/**
* fu_plugin_set_order:
* @self: a #FuPlugin
* @order: an integer value
*
* Sets the plugin order, where higher numbers are run after lower
* numbers.
*
* Since: 1.0.0
**/
void
fu_plugin_set_order(FuPlugin *self, guint order)
{
FuPluginPrivate *priv = fu_plugin_get_instance_private(self);
priv->order = order;
}
/**
* fu_plugin_get_priority:
* @self: a #FuPlugin
*
* Gets the plugin priority, where higher numbers are better.
*
* Returns: the integer value
*
* Since: 1.1.1
**/
guint
fu_plugin_get_priority(FuPlugin *self)
{
FuPluginPrivate *priv = fu_plugin_get_instance_private(self);
return priv->priority;
}
/**
* fu_plugin_set_priority:
* @self: a #FuPlugin
* @priority: an integer value
*
* Sets the plugin priority, where higher numbers are better.
*
* Since: 1.0.0
**/
void
fu_plugin_set_priority(FuPlugin *self, guint priority)
{
FuPluginPrivate *priv = fu_plugin_get_instance_private(self);
priv->priority = priority;
}
/**
* fu_plugin_add_rule:
* @self: a #FuPlugin
* @rule: a plugin rule, e.g. %FU_PLUGIN_RULE_CONFLICTS
* @name: a plugin name, e.g. `upower`
*
* If the plugin name is found, the rule will be used to sort the plugin list,
* for example the plugin specified by @name will be ordered after this plugin
* when %FU_PLUGIN_RULE_RUN_AFTER is used.
*
* NOTE: The depsolver is iterative and may not solve overly-complicated rules;
* If depsolving fails then fwupd will not start.
*
* Since: 1.0.0
**/
void
fu_plugin_add_rule(FuPlugin *self, FuPluginRule rule, const gchar *name)
{
FuPluginPrivate *priv = fu_plugin_get_instance_private(self);
if (priv->rules[rule] == NULL)
priv->rules[rule] = g_ptr_array_new_with_free_func(g_free);
g_ptr_array_add(priv->rules[rule], g_strdup(name));
g_signal_emit(self, signals[SIGNAL_RULES_CHANGED], 0);
}
/**
* fu_plugin_get_rules:
* @self: a #FuPlugin
* @rule: a plugin rule, e.g. %FU_PLUGIN_RULE_CONFLICTS
*
* Gets the plugin IDs that should be run after this plugin.
*
* Returns: (element-type utf8) (transfer none) (nullable): the list of plugin names, e.g.
*`['appstream']`
*
* Since: 1.0.0
**/
GPtrArray *
fu_plugin_get_rules(FuPlugin *self, FuPluginRule rule)
{
FuPluginPrivate *priv = fu_plugin_get_instance_private(self);
g_return_val_if_fail(rule < FU_PLUGIN_RULE_LAST, NULL);
return priv->rules[rule];
}
/**
* fu_plugin_add_report_metadata:
* @self: a #FuPlugin
* @key: a string, e.g. `FwupdateVersion`
* @value: a string, e.g. `10`
*
* Sets any additional metadata to be included in the firmware report to aid
* debugging problems.
*
* Any data included here will be sent to the metadata server after user
* confirmation.
*
* Since: 1.0.4
**/
void
fu_plugin_add_report_metadata(FuPlugin *self, const gchar *key, const gchar *value)
{
FuPluginPrivate *priv = fu_plugin_get_instance_private(self);
if (priv->report_metadata == NULL) {
priv->report_metadata =
g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
}
g_hash_table_insert(priv->report_metadata, g_strdup(key), g_strdup(value));
}
/**
* fu_plugin_get_report_metadata:
* @self: a #FuPlugin
*
* Returns the list of additional metadata to be added when filing a report.
*
* Returns: (transfer none) (nullable): the map of report metadata
*
* Since: 1.0.4
**/
GHashTable *
fu_plugin_get_report_metadata(FuPlugin *self)
{
FuPluginPrivate *priv = fu_plugin_get_instance_private(self);
return priv->report_metadata;
}
/**
* fu_plugin_get_config_value:
* @self: a #FuPlugin
* @key: a settings key
*
* Return the value of a key, falling back to the default value.
*
* Since: 1.0.6
**/
gchar *
fu_plugin_get_config_value(FuPlugin *self, const gchar *key)
{
FuPluginPrivate *priv = fu_plugin_get_instance_private(self);
FuConfig *config = fu_context_get_config(priv->ctx);
const gchar *name;
g_return_val_if_fail(FU_IS_PLUGIN(self), NULL);
g_return_val_if_fail(key != NULL, NULL);
if (config == NULL) {
g_critical("cannot get config value with no loaded context!");
return NULL;
}
name = fu_plugin_get_name(self);
if (name == NULL) {
g_critical("cannot get config value with no plugin name!");
return NULL;
}
return fu_config_get_value(config, name, key);
}
/**
* fu_plugin_set_config_default:
* @self: a #FuPlugin
* @key: a settings key
* @value: the default value of the key if not found
*
* Sets the config default value.
*
* Since: 2.0.0
**/
void
fu_plugin_set_config_default(FuPlugin *self, const gchar *key, const gchar *value)
{
FuPluginPrivate *priv = fu_plugin_get_instance_private(self);
FuConfig *config = fu_context_get_config(priv->ctx);
const gchar *name;
g_return_if_fail(FU_IS_PLUGIN(self));
g_return_if_fail(key != NULL);
if (config == NULL) {
g_critical("cannot set config default with no loaded context!");
return;
}
name = fu_plugin_get_name(self);
if (name == NULL) {
g_critical("cannot set config default with no plugin name!");
return;
}
fu_config_set_default(config, name, key, value);
}
/**
* fu_plugin_security_attr_new:
* @self: a #FuPlugin
* @appstream_id: (nullable): the AppStream component ID, e.g. `com.intel.BiosGuard`
*
* Creates a new #FwupdSecurityAttr for this specific plugin.
*
* Returns: (transfer full): a #FwupdSecurityAttr
*
* Since: 1.8.4
**/
FwupdSecurityAttr *
fu_plugin_security_attr_new(FuPlugin *self, const gchar *appstream_id)
{
FuPluginPrivate *priv = fu_plugin_get_instance_private(self);
g_autoptr(FwupdSecurityAttr) attr = NULL;
g_return_val_if_fail(FU_IS_PLUGIN(self), NULL);
g_return_val_if_fail(appstream_id != NULL, NULL);
attr = fu_security_attr_new(priv->ctx, appstream_id);
fwupd_security_attr_set_plugin(attr, fu_plugin_get_name(self));
return g_steal_pointer(&attr);
}
/**
* fu_plugin_set_config_value:
* @self: a #FuPlugin
* @key: a settings key
* @value: (nullable): a settings value
* @error: (nullable): optional return location for an error
*
* Sets a plugin config value.
*
* Returns: %TRUE for success
*
* Since: 1.7.0
**/
gboolean
fu_plugin_set_config_value(FuPlugin *self, const gchar *key, const gchar *value, GError **error)
{
FuPluginPrivate *priv = fu_plugin_get_instance_private(self);
FuConfig *config = fu_context_get_config(priv->ctx);
const gchar *name;
g_return_val_if_fail(FU_IS_PLUGIN(self), FALSE);
g_return_val_if_fail(key != NULL, FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
if (config == NULL) {
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_INTERNAL,
"cannot get config value with no loaded context");
return FALSE;
}
name = fu_plugin_get_name(self);
if (name == NULL) {
g_critical("cannot get config value with no plugin name!");
return FALSE;
}
return fu_config_set_value(config, name, key, value, error);
}
/**
* fu_plugin_reset_config_values:
* @self: a #FuPlugin
* @error: (nullable): optional return location for an error
*
* Reset all the plugin keys back to the default values.
*
* Returns: %TRUE for success
*
* Since: 1.9.15
**/
gboolean
fu_plugin_reset_config_values(FuPlugin *self, GError **error)
{
FuPluginPrivate *priv = fu_plugin_get_instance_private(self);
FuConfig *config = fu_context_get_config(priv->ctx);
const gchar *name;
g_return_val_if_fail(FU_IS_PLUGIN(self), FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
if (config == NULL) {
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_INTERNAL,
"cannot reset config values with no loaded context");
return FALSE;
}
name = fu_plugin_get_name(self);
if (name == NULL) {
g_critical("cannot reset config values with no plugin name!");
return FALSE;
}
return fu_config_reset_defaults(config, name, error);
}
/**
* fu_plugin_get_config_value_boolean:
* @self: a #FuPlugin
* @key: a settings key
*
* Return the boolean value of a key if it's been configured
*
* Returns: %TRUE if the value is `true` (case insensitive), %FALSE otherwise
*
* Since: 1.4.0
**/
gboolean
fu_plugin_get_config_value_boolean(FuPlugin *self, const gchar *key)
{
FuPluginPrivate *priv = fu_plugin_get_instance_private(self);
FuConfig *config = fu_context_get_config(priv->ctx);
const gchar *name;
g_return_val_if_fail(FU_IS_PLUGIN(self), FALSE);
g_return_val_if_fail(key != NULL, FALSE);
if (config == NULL) {
g_critical("cannot get config value with no loaded context!");
return FALSE;
}
name = fu_plugin_get_name(self);
if (name == NULL) {
g_critical("cannot get config value with no plugin name!");
return FALSE;
}
return fu_config_get_value_bool(config, name, key);
}
/**
* fu_plugin_name_compare:
* @plugin1: first #FuPlugin to compare.
* @plugin2: second #FuPlugin to compare.
*
* Compares two plugins by their names.
*
* Returns: 1, 0 or -1 if @plugin1 is greater, equal, or less than @plugin2.
*
* Since: 1.0.8
**/
gint
fu_plugin_name_compare(FuPlugin *plugin1, FuPlugin *plugin2)
{
return g_strcmp0(fu_plugin_get_name(plugin1), fu_plugin_get_name(plugin2));
}
/**
* fu_plugin_order_compare:
* @plugin1: first #FuPlugin to compare.
* @plugin2: second #FuPlugin to compare.
*
* Compares two plugins by their depsolved order, and then by name.
*
* Returns: 1, 0 or -1 if @plugin1 is greater, equal, or less than @plugin2.
*
* Since: 1.0.8
**/
gint
fu_plugin_order_compare(FuPlugin *plugin1, FuPlugin *plugin2)
{
FuPluginPrivate *priv1 = fu_plugin_get_instance_private(plugin1);
FuPluginPrivate *priv2 = fu_plugin_get_instance_private(plugin2);
if (priv1->order < priv2->order)
return -1;
if (priv1->order > priv2->order)
return 1;
return fu_plugin_name_compare(plugin1, plugin2);
}
static gchar *
fu_plugin_convert_gtype_to_name(GType gtype)
{
const gchar *gtype_name = g_type_name(gtype);
gsize len = strlen(gtype_name);
g_autoptr(GString) str = g_string_new(NULL);
g_return_val_if_fail(g_str_has_prefix(gtype_name, "Fu"), NULL);
g_return_val_if_fail(g_str_has_suffix(gtype_name, "Plugin"), NULL);
/* self tests */
if (g_strcmp0(gtype_name, "FuPlugin") == 0)
return g_strdup("plugin");
/* normal plugins */
for (guint j = 2; j < len - 6; j++) {
gchar tmp = gtype_name[j];
if (g_ascii_isupper(tmp)) {
if (str->len > 0)
g_string_append_c(str, '_');
g_string_append_c(str, g_ascii_tolower(tmp));
} else {
g_string_append_c(str, tmp);
}
}
if (str->len == 0)
return NULL;
return g_string_free(g_steal_pointer(&str), FALSE);
}
static void
fu_plugin_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
FuPlugin *self = FU_PLUGIN(object);
FuPluginPrivate *priv = GET_PRIVATE(self);
switch (prop_id) {
case PROP_CONTEXT:
g_value_set_object(value, priv->ctx);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static void
fu_plugin_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
FuPlugin *self = FU_PLUGIN(object);
switch (prop_id) {
case PROP_CONTEXT:
fu_plugin_set_context(self, g_value_get_object(value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static void
fu_plugin_dispose(GObject *object)
{
FuPlugin *self = FU_PLUGIN(object);
FuPluginPrivate *priv = GET_PRIVATE(self);
if (priv->devices != NULL)
g_ptr_array_set_size(priv->devices, 0);
if (priv->cache != NULL)
g_hash_table_remove_all(priv->cache);
g_clear_object(&priv->ctx);
G_OBJECT_CLASS(fu_plugin_parent_class)->dispose(object);
}
static void
fu_plugin_class_init(FuPluginClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS(klass);
GParamSpec *pspec;
object_class->finalize = fu_plugin_finalize;
object_class->dispose = fu_plugin_dispose;
object_class->get_property = fu_plugin_get_property;
object_class->set_property = fu_plugin_set_property;
/**
* FuPlugin::device-added:
* @self: the #FuPlugin instance that emitted the signal
* @device: the #FuDevice
*
* The ::device-added signal is emitted when a device has been added by the plugin.
*
* Since: 0.8.0
**/
signals[SIGNAL_DEVICE_ADDED] = g_signal_new("device-added",
G_TYPE_FROM_CLASS(object_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET(FuPluginClass, _device_added),
NULL,
NULL,
g_cclosure_marshal_VOID__OBJECT,
G_TYPE_NONE,
1,
FU_TYPE_DEVICE);
/**
* FuPlugin::device-removed:
* @self: the #FuPlugin instance that emitted the signal
* @device: the #FuDevice
*
* The ::device-removed signal is emitted when a device has been removed by the plugin.
*
* Since: 0.8.0
**/
signals[SIGNAL_DEVICE_REMOVED] =
g_signal_new("device-removed",
G_TYPE_FROM_CLASS(object_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET(FuPluginClass, _device_removed),
NULL,
NULL,
g_cclosure_marshal_VOID__OBJECT,
G_TYPE_NONE,
1,
FU_TYPE_DEVICE);
/**
* FuPlugin::device-register:
* @self: the #FuPlugin instance that emitted the signal
* @device: the #FuDevice
*
* The ::device-register signal is emitted when another plugin has added the device.
*
* Since: 0.9.7
**/
signals[SIGNAL_DEVICE_REGISTER] =
g_signal_new("device-register",
G_TYPE_FROM_CLASS(object_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET(FuPluginClass, _device_register),
NULL,
NULL,
g_cclosure_marshal_VOID__OBJECT,
G_TYPE_NONE,
1,
FU_TYPE_DEVICE);
/**
* FuPlugin::check-supported:
* @self: the #FuPlugin instance that emitted the signal
* @guid: a device GUID
*
* The ::check-supported signal is emitted when a plugin wants to ask the daemon if a
* specific device GUID is supported in the existing system metadata.
*
* Returns: %TRUE if the GUID is found
*
* Since: 1.0.0
**/
signals[SIGNAL_CHECK_SUPPORTED] =
g_signal_new("check-supported",
G_TYPE_FROM_CLASS(object_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET(FuPluginClass, _check_supported),
NULL,
NULL,
g_cclosure_marshal_generic,
G_TYPE_BOOLEAN,
1,
G_TYPE_STRING);
signals[SIGNAL_RULES_CHANGED] = g_signal_new("rules-changed",
G_TYPE_FROM_CLASS(object_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET(FuPluginClass, _rules_changed),
NULL,
NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE,
0);
/**
* FuPlugin:context:
*
* The #FuContext to use.
*
* Since: 1.8.6
*/
pspec = g_param_spec_object("context",
NULL,
NULL,
FU_TYPE_CONTEXT,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_NAME);
g_object_class_install_property(object_class, PROP_CONTEXT, pspec);
}
static void
fu_plugin_init(FuPlugin *self)
{
FuPluginPrivate *priv = GET_PRIVATE(self);
priv->device_gtype_default = G_TYPE_INVALID;
}
static void
fu_plugin_finalize(GObject *object)
{
FuPlugin *self = FU_PLUGIN(object);
FuPluginPrivate *priv = GET_PRIVATE(self);
FuPluginVfuncs *vfuncs = fu_plugin_get_vfuncs(self);
/* optional */
if (priv->done_init && vfuncs->finalize != NULL) {
g_debug("finalize(%s)", fu_plugin_get_name(self));
vfuncs->finalize(G_OBJECT(self));
}
for (guint i = 0; i < FU_PLUGIN_RULE_LAST; i++) {
if (priv->rules[i] != NULL)
g_ptr_array_unref(priv->rules[i]);
}
if (priv->devices != NULL)
g_ptr_array_unref(priv->devices);
if (priv->runtime_versions != NULL)
g_hash_table_unref(priv->runtime_versions);
if (priv->compile_versions != NULL)
g_hash_table_unref(priv->compile_versions);
if (priv->report_metadata != NULL)
g_hash_table_unref(priv->report_metadata);
if (priv->cache != NULL)
g_hash_table_unref(priv->cache);
if (priv->device_gtypes != NULL)
g_array_unref(priv->device_gtypes);
if (priv->config_monitor != NULL)
g_object_unref(priv->config_monitor);
g_free(priv->data);
G_OBJECT_CLASS(fu_plugin_parent_class)->finalize(object);
}
/**
* fu_plugin_new_from_gtype:
* @ctx: (nullable): a #FuContext
* @gtype: a #GType, possibly even `G_TYPE_PLUGIN`
*
* Creates a new #FuPlugin
*
* Since: 1.8.6
**/
FuPlugin *
fu_plugin_new_from_gtype(GType gtype, FuContext *ctx)
{
FuPlugin *self;
g_return_val_if_fail(gtype != G_TYPE_INVALID, NULL);
g_return_val_if_fail(ctx == NULL || FU_IS_CONTEXT(ctx), NULL);
self = g_object_new(gtype, "context", ctx, NULL);
if (fu_plugin_get_name(self) == NULL) {
g_autofree gchar *name = fu_plugin_convert_gtype_to_name(gtype);
fu_plugin_set_name(self, name);
}
return self;
}
/**
* fu_plugin_new:
* @ctx: (nullable): a #FuContext
*
* Creates a new #FuPlugin
*
* Since: 0.8.0
**/
FuPlugin *
fu_plugin_new(FuContext *ctx)
{
FuPlugin *self = FU_PLUGIN(g_object_new(FU_TYPE_PLUGIN, NULL));
if (ctx != NULL)
fu_plugin_set_context(self, ctx);
return self;
}
|